LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-10-2005, 04:11 AM   #1
p0tw0r
LQ Newbie
 
Registered: Apr 2005
Posts: 8

Rep: Reputation: 0
Getting part of variable


I would like to echo some specific fields from a more complex variable.
In windows i used:

echo %var1%
result - my_var

echo %var1:~4,2%
result - ar

How do I do it in bash?

I posted it before on newbe and was suggested to use

cut -f

but I can't get it to work...
 
Old 04-10-2005, 04:39 AM   #2
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Code:
skalkoto@darkstar:~/src$ var1=my_var
skalkoto@darkstar:~/src$ echo $var1
my_var
skalkoto@darkstar:~/src$ echo `expr substr $var1 5 2`
ar
skalkoto@darkstar:~/src$
for more info on string manipulation in bash read here:
http://www.tldp.org/LDP/abs/html/str...ipulation.html
 
Old 04-10-2005, 04:52 AM   #3
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
cut is for cutting the input into fields according to a delimiter ( -d) and displaying specific fields ( -f)

Code:
skalkoto@darkstar:~$ echo "my name is john" | cut -d " " -f 2,4
name john
skalkoto@darkstar:~$
 
Old 04-11-2005, 02:25 AM   #4
p0tw0r
LQ Newbie
 
Registered: Apr 2005
Posts: 8

Original Poster
Rep: Reputation: 0
Thx! It works perfectly.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Print a part of variable/string p0tw0r Linux - Newbie 1 04-07-2005 02:49 PM
bash, how to get variable name from variable Duudson Programming 6 01-06-2005 04:38 PM
unsetting a part of ENV variable praveenv Linux - Newbie 3 08-28-2004 09:36 AM
calling a variable within a variable sdandeker Programming 9 04-28-2004 03:55 PM
How to get 'Window' variable from a 'Widget' variable bordel Programming 0 11-19-2003 03:19 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:13 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration