I am having the below shell script  var="this is a test"  ls -ltr| while read file do      echo $var done echo $var   I am getting the below output:  this is a test this is a test this is a test   How am I getting the value of the variable "var" set to "this is a test" inside while loop since piping will spawn a new sub-shell and I am also not exporting my "var" variable in the main shell?  As far as I know, in order for the child to inherit the variable values from the parent shell, we need to export the variable, but in this case the variable value is getting inherited without the "export" statement.
12724
Why do I get different values for $x from the snippets below?  #!/bin/bash  x=1 echo fred &gt; junk ; while read var ; do x=55 ; done &lt; junk echo x=$x  #    x=55 .. I'd expect this result  x=1 cat junk | while read var ; do x=55 ; done echo x=$x  #    x=1 .. but why?  x=1 echo fred | while read var ; do x=55 ; done echo x=$x  #    x=1  .. but why?
63360
I'm running Ubuntu 11.04. I use the terminal to start a bash session, and I want to add an environment variable:  $r@hajt:~$ env THEVAR=/example   But it's not working. It shows all the variables with THEVAR being the last one, but another call to env does not show THEVAR anymore- env | grep THEVAR returns nothing.  Similarly, scripts with export (export THEVAR=/example) or other variable assignments (THEVAR=/example) don't add the environment variable.  I know I'm doing something wrong, I know it should be something simple, but I just can't find what.  UPDATE: The real meaning of my question was this one:   (Anyway I'll choose the most voted answer and leave the edited title -that wasn't what I was asking)  env runs a program in a modified environment, then dismisses all the changes.
50062
I've got a script 'myscript' that outputs the following:  abc def ghi   in another script, I call:  declare RESULT=$(./myscript)   and $RESULT gets the value  abc def ghi   Is there a way to store the result either with the newlines, or with '\n' character so I can output it with 'echo -e'?
45950
I'm writing a script and I would like to pass the results from bc into a variable. I've declared 2 variables (var1 and var2) and have given them values.  In my script I want to pass the results from bc into another variable say var3 so that I can work with var3 for other calculations.  So far I have been able write the result to a file which is not what I'm looking for and also I've been able to echo the result in the terminal but I just want to pass the result to a variable at moment so that I can work with that variable.  echo "scale=2;$var1/var2" | bc
11347
I read  that the purpose of export in a shell is to make the variable available to sub-processes started from the shell.  However, I have also read  and  that "Processes inherit their environment from their parent (the process which started them)."  If this is the case, why do we need export? What am  I missing?  Are shell variables not part of the environment by default? What is the difference?
118424
I'm used to bash's builtin read function in while loops, e.g.:  echo "0 1       1 1       1 2       2 3" |\ while read A B; do     echo $A + $B | bc; done   I've been working on some make project, and it became prudent to split files and store intermediary results. As a consequence I often end up shredding single lines into variables. While the following example works pretty well,  head -n1 somefile | while read A B C D E FOO; do [... use vars here ...]; done   it's sort of stupid, because the while loop will never run more than once. But without the while,  head -n1 somefile | read A B C D E FOO; [... use vars here ...]   The read variables are always empty when I use them. I never noticed this behaviour of read, because usually I'd use while loops to process many similar lines. How can I use bash's read builtin without a while loop? Or is there another (or even better) way to read a single line into multiple (!) variables?  Conclusion  The answers teach us, it's a problem of scoping. The statement   cmd0; cmd1; cmd2 | cmd3; cmd4   is interpreted such that the commands cmd0, cmd1, and cmd4 are executed in the same scope, while the commands cmd2 and cmd3 are each given their own subshell, and consequently different scopes. The original shell is the parent of both subshells.
35975
The variable BUILDNUMBER is set to value 230. I expect 230_ to be printed for the command echo $BUILDNUMBER_ but the output is empty as shown below.  # echo $BUILDNUMBER_  # echo $BUILDNUMBER 230
63089
I need to indirectly reference a variable in the bash shell.  I basically want to what you can do in make by writing $($(var)).  I have tried using ${$var} which would be the most straight forward solution in bash but then I get this error:  bash: ${$var}: bad substitution   Is there a way to do this?  What I am trying to do is to iterate over all the arguments ($1, $2, $3, ...) to a program using an iteration variable and I cannot do this without indirection.
2542
Having the following in one of my shell functions:  function _process () {   awk -v l="$line" '   BEGIN {p=0}   /'"$1"'/ {p=1}   END{ if(p) print l &gt;&gt; "outfile.txt" }   ' }   , so when called as _process $arg, $arg gets passed as $1, and used as a search pattern. It works this way, because shell expands $1 in place of awk pattern! Also l can be used inside awk program, being declared with -v l="$line". All fine.  Is it possible in same manner give pattern to search as a variable?  Following will not work,  awk -v l="$line" -v search="$pattern" '   BEGIN {p=0}   /search/ {p=1}   END{ if(p) print l &gt;&gt; "outfile.txt" }   '   ,as awk will not interpret /search/ as a variable, but instead literally.
55413
The question is pretty straight forward. I can get the exit code of the previous command by using $?. But what exactly is it?  I don't think it is a variable, because you can't have any special characters in the name of variable other than _.  foo?foo=10   will result in the error: foo?foo=10: command not found  So if it is not a variable, what is it? Are there others like it?
25759
I have encountered comparisions of variables to string literals multiple times over the years which had one character prefixing the variable and the literal, e.g.  if [ "x$A" = "xtrue" ]; then   in order to check whether $A is "true".  I assume this is done to achieve shell compatibility or to work around a longterm bug, an unintuitive behavior, etc. Nothing obvious comes to mind.  Today I figured I want to know the reason, but my research didn't turn up anything. Or maybe it's just me making something out of a rather frequent exposure to rare occurances.  Is this practice still useful, maybe even best?
109280
Consider the two shell samples  $ ls myDoc.html SomeDirectory someDoc.txt   and  $ echo $(ls) myDoc.html SomeDirectory someDoc.txt   The first executes ls which, as I understand, appends the contents of the current working directory to the stdout file (which is what the terminal displays).  Is this correct?  The second takes the value of the ls command (which means is the contents of the current working directory) and prints it to the stdout file. Is this correct?  Why do the two commands give different outputs?
100638
As a simple example, I have a bunch of source code files. I want to store the "head" command output to a variable for all these files.  I tried:  output=$(head $file)   but what happened is that this automatically trimmed all \n characters when storing the output to a variable.  How do I store the command output as is without removing \n characters?
28663
I found this line in a korn shell script:  var=${var:-/temp}   I am able to figure it out what it is doing   $ var=/data $ var=${var:-/temp} $ echo $var /data $ var= $ echo $var  $ var=${var:-/temp} $ echo $var /temp   Does it have advantage over checking the value and then assigning?
64067
I've written the following script to set some environment variables when needed.  #!/bin/sh export BASE=/home/develop/trees echo $BASE export PATH=$PATH:$BASE echo $PATH   Below the command and the results I can see on my terminal: the script runs, but the variables are not set at the end.  ~$: ./script.sh /home/develop/trees /bin:......:/home/develop/trees ~$: echo $BASE  ~$:    What's wrong? Thanks in advance. Mirko
14201
I'm not giving the real example as the post becomes so long that no one would read it. So I'll give other examples I have a bash script name.sh and it is programmed as below- #!/bin/bash echo &quot;Please Enter your name&quot; read name echo &quot;Hello $name&quot; sudo bash age.sh  And another script(say age.sh) like this- #!/bin/bash *echo &quot;Hello $name&quot; echo &quot;Please enter your age&quot; read age echo &quot;Thank you&quot;  *Here in the second script I want the variable $name from the first script to be used with the same value in the second script so the user doesn't have to type his name again Of course if I'm declaring a variable then I can do it in the other script too but if the user is declaring it then I can't predict what the user will answer. So how do I transfer the same value over to the other script? I have another idea to do it It goes like this- #!/bin/bash echo &quot;Please Enter your name&quot; read name echo &quot;Hello $name&quot; sudo bash age.sh --variable_value  Somehow can i make the 2nd script use the values given after -- and assign the same variables to them?
48283
I am taking the Linux Essentials class and was doing well until I hit the scripting chapter. I simply do not understand these concepts.  Wondering if someone can break the following down into ultra simplistic terms or point me to a better reference to learn it. I'm currently using netacad's curriculum.        From the text book (with minor formatting changes):     There are some special variables in addition to the ones you set. You   can pass arguments to your script:  #!/bin/bash echo "Hello $1"       A dollar sign followed by a number N corresponds to the Nth argument   passed to the script. If you call the example above with ./test.sh the   output will be Hello Linux. The $0 variable contains the name of the   script itself.      After a program runs, be it a binary or a script, it returns an exit   code which is an integer between 0 and 255. You can test this through   the $? variable to see if the previous command completed successfully.     I understand how to assign variables and how they work with the $ but the whole issue with $0 and $1 -- I just don't get it.    Any help would be much appreciated.