LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2012, 12:27 PM   #1
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Rep: Reputation: Disabled
Referencing "list" item from environment variable in shell


Don't know how much of this is true, but I know I can assign different paths in PATH variable separated by colon ":"

I want to use this feature to split arbitrary string with colon as separator

For example, I assign:

Code:
X=one:two
Now I want to get first item - "one"

How can I reference it in shell, if possible?

I know about some approaches using IFS then looping to get split, but that's not what I'm after, as I need simplest possible form

TIA

Last edited by zx_; 01-20-2012 at 12:39 PM.
 
Old 01-20-2012, 12:39 PM   #2
zx_
LQ Newbie
 
Registered: Sep 2011
Posts: 23

Original Poster
Rep: Reputation: Disabled
OK, find my way out just after posting.

In sh I can use ${X%:*} and ${X#*:} after assigning X to get split of string with two parts (just like my problem)

For multiple split items perhaps there is some other solution


Cheers
 
Old 01-20-2012, 12:57 PM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,252

Rep: Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685Reputation: 2685
You a use the positional variables if they are not already set and you are not worried about overriding them:
Code:
set -- ${X//:/ }

echo $1
echo $2
 
Old 01-21-2012, 07:55 AM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
There are ever so many ways to split strings; parameter expansions, changing IFS, read, regexes inside [[..]]. Which is best to use really depends on how the line if formatted and what you intend to do with the output. Do you only need the first part, for example, or the ability to access any of the individual parts?

If you only need one part, then parameter expansion is usually the best. For random access of any of the parts, you should use something similar to what grail provided above. I'd usually go with a true array rather than the positional parameters (assuming a shell that supports them), however.

Code:
x="one:two:three"
parts=( ${x//:/ } )

echo "${parts[0]}"
echo "${parts[1]}"
echo "${parts[2]}"

#Another bash variation:

x="one:two:three and a half"
IFS=":" read -a parts <<<"$x"

echo "${parts[0]}"
echo "${parts[1]}"
echo "${parts[2]}"
Notice how the first technique would fail if any of the sub-parts contained whitespace, so changing IFS is safer overall.

See this page for many more string manipulation techniques:

string manipulation
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
environment variable issue in shell George2 Linux - Newbie 13 05-27-2010 12:24 PM
how can i change the value of a environment variable through shell script ? manomohan Programming 8 02-11-2010 10:55 PM
shell to set environment variable ufmale Linux - Newbie 5 11-16-2009 12:35 PM
what is the difference between CGI and SHELL environment variable cnsung Linux - Server 3 09-11-2009 11:36 AM
Global (shell) environment variable question Seventh Linux - Newbie 1 04-05-2007 04:18 PM


All times are GMT -5. The time now is 04:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration