Review your favorite Linux distribution.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 01-20-2012, 11:27 AM   #1
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:

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


Last edited by zx_; 01-20-2012 at 11:39 AM.
Old 01-20-2012, 11:39 AM   #2
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

Old 01-20-2012, 11:57 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,592

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

echo $1
echo $2
Old 01-21-2012, 06:55 AM   #4
David the H.
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian + kde 4 / 5
Posts: 6,837

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
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.

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


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

All times are GMT -5. The time now is 04:40 PM.

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