What does the command $() do in UNIX? Could someone explain what is the significance of $ in the below examples;  $(echo var=10)     and    eval $(echo var=10)
121396
What is $() in Linux Shell Commands?   For example:  chmod 777 $(pwd)
43101
Which one of $* or $@ is better to use (in array syntax,command line parameters, etc.) to avoid any bugs/problems in code? Or it does not make any difference?
110271
I am having trouble with bash. I am trying to put a command in an if statement and then compare it to a string.  This works perfectly.  echo $(ipcs | grep Shared | awk '{print $2}')   When I put it in an if statement I get some problems.  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" -eq "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" = "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false  $ if [ "$(ipcs | grep Shared | awk '{print $2}')" == "Shared"]; then   echo expression evaluated as true; else   echo expression evaluated as false; fi bash: [: missing `]' expression evaluated as false   I tried ==, =, and -eq because I wasn't sure which one to use.
778
Usually, $0 in a script is set to the name of the script, or to whatever it was invoked as (including the path). However, if I use bash with the -c option, $0 is set to the first of the arguments passed after the command string:  bash -c 'echo $0' foo bar  # foo    In effect, it seems like positional parameters have been shifted, but including $0. However shift in the command string doesn't affect $0 (as normal):  bash -c 'echo $0; shift; echo $0' foo bar # foo # foo   Why this apparently odd behaviour for command strings? Note that I am looking for the reason, the rationale, behind implementing such odd behaviour.     One could speculate that such a command string wouldn't need the $0 parameter as usually defined, so for economy it is also used for normal arguments. However, in that case the behaviour of shift is odd. Another possibility is that $0 is used to define the behaviour of programs (a la bash called as sh or vim called as vi), but that cannot be, since $0 here is only seen in the command string and not by programs called within it. I cannot think of any other uses for $0, so I am at a loss to explain this.
83723
What does ~$ stand for? For example:  user@ubuntu:~$   So far I know that the $ sign is a symbol for a regular user. If I'm not mistaken root have # at the end.   I also found a lot of commands in the forums that begin with $:  $ sudo apt-get update    So for what does $ actually stand for?  EDIT (28.01.2014) :  Today I came across this video that totally answers my question from 8 months ago and decided to share it here :)  BASH Basics -
46414
As per my knowledge, to determine the current shell we use echo $0 in the shell. Rather I want my script to check in which shell it is running. So, I tried to print $0 in the script and it returns the name of the script as it should. So, my question is how can I find which shell is my script running in during runtime?
37780
I have seen constructs in scripts such as this:  if somevar="$(somecommand 2&gt;/dev/null)"; then ... fi   Is this documented somewhere?  How is the return status of a variable determined and how does it relate to command substitution?  (For instance, would I get the same result with if echo "$(somecommand 2&gt;/dev/null)"; then?)
97244
From the bash manual, on the :     $? Expands to the exit status of the most recently executed   foreground pipeline.   I wonder why bash updates the $? variable on pressing Ctrl-C or Ctrl-Z:  $ echo $? 0 $ ^C $ echo $? 130 $ sleep 10 ^Z [1]+  Stopped                 sleep 10 $ echo $? 148
123679
Two function definitions, the only difference is that the first combines the local storage keyword with the assignment, while the second separates them:  function foo {     local fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  function bar {     local fn     fn=$(mktemp -p /path/does/not/exist 2&gt;/dev/null)     echo $? }  foo bar   This echoes "0" then "1". I expect it to echo "1" then "1". It seems like the value of $? is the result of the assignment to local, rather than the result of the command substitution.  Why does bash 4.2.46(1)-release behave this way?
43099
On the paragraph explaining arithmetic expansion,  uncovers 2 different ways of evaluating an expression, the first one uses $(( EXPRESSION )) and the second one uses $[ EXPRESSION ]. The two ways seem pretty similar as the only difference I have found is:     $[ EXPRESSION ] will only calculate the result of EXPRESSION, and do no tests:   Yet, I am intrigued because the same document recommends using $[ EXPRESSION ] rather than $(( EXPRESSION )).     Wherever possible, Bash users should try to use the syntax with square brackets:   Why would you want that if less tests are being done?
31437
In the Perl documentation,  suggests launching Perl scripts using a bilingual shell/Perl header:  #!/bin/sh #! -*-perl-*- eval 'exec perl -x -wS $0 ${1+"$@"}'     if 0;   What does ${1+"$@"} mean?  I tried using "$@" instead (using Bash as /bin/sh), and it seems to work just as well.    Edit  Two answers below say that it's supposed to be ${1:+"$@"}.  I am aware of the ${parameter:+word} ("Use Alternate Value") syntax documented in bash(1).  However, I am unconvinced, because   Both ${1+"$@"} and "$@" work just fine, even when there are no parameters.  If I create simple.sh as  #!/bin/sh eval 'exec /usr/bin/perl -x -S -- $0 "$@"'     if 0; #!perl use Data::Dumper; print Dumper(\@ARGV);   and question.sh as  #!/bin/sh eval 'exec /usr/bin/perl -x -S -- $0 ${1+"$@"}'     if 0; #!perl use Data::Dumper; print Dumper(\@ARGV);   I can get both to work identically:  $ ./question.sh  $VAR1 = []; $ ./question.sh a $VAR1 = [           'a'         ]; $ ./question.sh a 'b c' $VAR1 = [           'a',           'b c'         ]; $ ./question.sh "" $VAR1 = [           ''         ]; $ ./simple.sh  $VAR1 = []; $ ./simple.sh a $VAR1 = [           'a'         ]; $ ./simple.sh a 'b c' $VAR1 = [           'a',           'b c'         ]; $ ./simple.sh "" $VAR1 = [           ''         ];  Other sources on the Internet also use ${1+"$@"}, including  who seems to know what he's doing.   Perhaps ${parameter+word} is an undocumented alternate (or deprecated) syntax for ${parameter:+word}?  Could someone confirm that hypothesis?
69637
i am writing a script in bash and I would like to give the two numbers in the brackets, x and y to be precise, as variables.  I wrote the script this way:  echo "x?" read x echo "y" read y  for i in {$x..$y}   but it does not work.  How could I set it up to make it function?  Thanks a lot in advance.
100567
This is from . It's about executing a Perl script.     Finally, if you are unfortunate enough to be on an ancient Unix system   that doesn’t support the magic #! line, or if the path to your   interpreter is longer than 32 characters (a built-in limit on many   systems), you may be able to work around it like this:   #!/bin/sh -- # perl, to stop looping eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' if 0;   Can you explain step by step what's going on here? I'm trying to make it work or encompass, but to no avail.  At execution of just the above, I'm getting this:  /bin/sh: 0: Illegal option --
90781
I want to replace cat:  var=$(cat "filename" 2&gt;/dev/null)   by bashism syntax:  var=$(&lt;"filename")   The problem is that I don't know how to make the bashism silent to avoid such warnings:  bash: filename: No such file or directory   I've tried this:  var=$(2&gt;/dev/null &lt;"filename")   but it does not read existing files into var anymore.
55899
I have written a small script to count number of occurrences of logs in a particular file. The following is my code:   #!/bin/sh count=1 while read line do     found=$( grep M $line )     OCCURENCE=$( grep M /log.txt)     if [ "$OCCURENCE" ]; then         echo $count")" $line &gt;&gt; filesave.txt     fi     count=$((count+1)) done &lt; log1.txt   but I am getting this error:  ./count.sh ./count.sh: syntax error at line 10: `found=$' unexpected
23221
The modern recommendation is to use:  $(...)   instead of the older:  `...`   mainly for readability and to avoid the need for escaping when nesting.  Which shells do not support the $(...) form, and are any of them still in use today?
54303
If you take this code:  echo -e '\t\t\tString' | grep '^[\t]*String'   the result is blank because it doesn't match, yet this:  echo -e '\t\t\tString' | grep $'^[\t]*String'   works. I swear that I must have used the first line's code a hundred times in my scripts and in the terminal, without ever using the "$" character like that, and it's always seemed to work. Has there been some recent change? Why does it need the "$" character? Or am I doing something wrong?