Bash - concatenate string - strange variable scoping


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash - concatenate string - strange variable scoping
# 1  
Old 09-01-2016
Bash - concatenate string - strange variable scoping

Hello,

I am trying to concatenate a string in a bash script like this:
Code:
runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" "

env | grep "$ENV_SUFFIX" | while read line; do
  envCmd="-e \"${line}\" "
  runCmd=$runCmd$envCmd
  echo $runCmd # here concatenation works fine
done

echo $runCmd # here concatenation disappears

Inside the loop, concatenation works fine, however when I try to echo the result outside of it, it disappears. Any idea how to make it work in bash? Thanks a lot
# 2  
Old 09-01-2016
Hi,
Your problem is the pipe ( '|' ) that create another process to execute your filter.
In bash, you can change your syntax like this (but not posix) :
Code:
runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" "
while read line; do
   envCmd="-e \"${line}\" "
   runCmd=$runCmd$envCmd
   echo $runCmd # here concatenation works fine
done < <(env | grep "$ENV_SUFFIX" )
echo $runCmd # here concatenation disappears

Regards.
This User Gave Thanks to disedorgue For This Post:
# 3  
Old 09-01-2016
Works, thanks a lot.
# 4  
Old 09-01-2016
In POSIX, it is unspecified whether or not the last stage of a pipeline is run in the current shell execution environment. (In ksh it is and the script in post #1 would work without change; in bash it is not and the value of a variable set in the last stage of the pipeline disappears when the pipeline completes execution.)

With any POSIX conforming shell, you could modify disedorgue's suggestion to:
Code:
runCmd="docker run -e \"IMAGE_NAME=$IMAGE_NAME\" "
while read line; do
   envCmd="-e \"${line}\" "
   runCmd=$runCmd$envCmd
   echo $runCmd # here concatenation works fine
done <<EOF
$(env | grep "$ENV_SUFFIX")
EOF
echo $runCmd # and concatenation works here too

and get what you want.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing string as variable(s) in bash

I'm trying to write a basic bash script that takes input you give (what directory, if any, what name, if any ....) and passes the information to find. I'm trying to just create a string with all variables and then pass it to find. So far I have this extremely simple: #!/bin/bash -f ... (2 Replies)
Discussion started by: Starting_Leaf
2 Replies

2. Shell Programming and Scripting

bash: using a string variable in grep

Hi, I've been stuck for several days on this. Using grep on a command line, I can use quotes, eg... grep 'pattern of several words' filename I want to do this in my bash script. In my script I have captured the several command line arguments (eg arg1 arg2) into a variable: variable=$@ I... (2 Replies)
Discussion started by: adrian777uk
2 Replies

3. Shell Programming and Scripting

Search for string in a file, extract two another strings and concatenate to a variable

I have a file with <suit:run date="Trump Tue 06/19/2012 11:41 AM EDT" machine="garg-ln" build="19921" level="beta" release="6.1.5" os="Linux"> Need to find word "build" then extract build number, which is 19921 also release number, which is 6.1.5 then concatenate them to one variable as... (6 Replies)
Discussion started by: garg
6 Replies

4. UNIX for Dummies Questions & Answers

bash variable string declaration

Hello, Why is this not working in a script? files="test.fsa" echo $files for file in $files do if then echo "$file does not exist." fi run a command done I get an error saying (3 Replies)
Discussion started by: verse123
3 Replies

5. Shell Programming and Scripting

Bash assign string to variable

Hi ,I am trying to assign string to variable ,but it doesn't work Also could you show me different ways to use grep,(I am trying to get the first,second and first column form file,and I am counting the chars) let name=`grep "$id" product | cut -c6-20` (25 Replies)
Discussion started by: lio123
25 Replies

6. Shell Programming and Scripting

Shell variable scoping

This may be a stupid question, but was wondering if it is possible to make a variable local to a particular script and invisible to an external script that may source the script where it is defined? Thanks as always (2 Replies)
Discussion started by: stevensw
2 Replies

7. UNIX for Dummies Questions & Answers

Concatenate a string to a variable

Hello All, How to concatenate a string to a variable in a script I'm having a file which is consisting of data and i need to extract the first line of the file and append it to a string. /tmp/samp.list containg 60000 I like to concatenate it with a string (SS_) grep -w SS_$(head -1... (1 Reply)
Discussion started by: nkamalkishore
1 Replies

8. Shell Programming and Scripting

Bash string variable manipulation

In a bash script I've set a variable that is the directory name of where an executable lives. the_dir=`dirname $which myscript` which equates to something like "/path/to/dir/bin" I need to cut that down to remove the "bin" so I now have "/path/to/dir/". This sounds easy but as a... (2 Replies)
Discussion started by: Witty
2 Replies

9. Shell Programming and Scripting

How to concatenate a string and a variable

I need a way to build variable in this manner: variable_$i Inside a for loop i need to create it. where i goes from 1 to 30.. and then i need to print them on screen with echo $variable_$i which is the best way to do this? (6 Replies)
Discussion started by: sreedivia
6 Replies

10. Shell Programming and Scripting

ksh/Linux: Variable scoping issue? Pl. help!

user_account() { set -x nodename=$1 # set userid to user0 userid="user0" echo outside:pid $$ cat $MY_DIR/user_accounts | while read line do # line="node1 user1" echo inside do: pid $$ line:$line userid:$userid poss_node=`echo $line |awk '{print $1}'`... (2 Replies)
Discussion started by: jasmeet100
2 Replies
Login or Register to Ask a Question