I have this function that echoes the recent directory  funtion prompt_oldpwd() {   local oldpwd="";      # check if the $OLDPWD is set and not empty     if [ ! -z "${OLDPWD}" ]; then         # truncate $OLDPWD value and set it to oldpwd        oldpwd="${OLDPWD##*/}";      else      return;      fi  echo "${oldpwd}";  }   if the $OLDPWD variable is set and not null it prints the truncated value.   It works fine when I call the function inside the terminal but when use in a prompt like this:  export PS1="$(prompt_oldpwd)"   it actually prints the value in the prompt but if the $OLDPWD change it won't update the prompt unless I run bash again.. Why is it like that? and how do I make it that when the $OLDPWD change it should also update?
120827
I have the following prompt in bash which shows the current git branch:  PS1+="$(git_prompt)" #git_prompt is a function in my .bashrc   which works when I source the .bashrc, but not when I change the branch, so the PS1 var gets only evaluated when I source the .bashrc, but it should be evaluated every time a new prompt is displayed. How can this be accomplished with bash 4.3 ?
48113
Consider this snippet:  stop () {     echo "${1}" 1&gt;&amp;2     exit 1 }  func () {     if false; then         echo "foo"     else         stop "something went wrong"     fi }   Normally when func is called it will cause the script to terminate, which is the intended behaviour. However, if it's executed in a sub-shell, such as in  result=`func`   it will not exit the script. This means the calling code has to check the exit status of the function every time. Is there a way to avoid this? Is this what set -e is for?
38314
touch ~/deleted/$(echo "directory_"$(readlink -f foo)|tr '/' '\') 2&gt; /dev/null   where foo is a directory  It's reading the full pathname of foo and creating it as a file replacing all '/' with '\' and putting a directory_ in front so the directory  /home/test/foo would create a file directory_\home\test\foo  It does exactly what I want except a warning keeps printing tr: warning: an unescaped backslash at end of string is not portable   a. I don't know what that means b. I would like it to not show  I tried to redirect stderr to /dev/null but it doesn't work. How do I suppress it?
42820
When going through one shell script, I saw the term "$?". What is the significance of this term?
32412
When using the terminal in a deep folder structure sometimes the prompt can take up most of the line.  Is there any way in which I can trim the working directory?  I know I can do  PS1="\W &gt;"   to only print the current directory and not the full path, but is there a way to have something like:  /home/smauel/de...ther/folder &gt;
9050
I have a shell script with this code:  var=`hg st -R "$path"` if [ -n "$var" ]; then     echo $var fi   But the conditional code always executes, because hg st always prints at least one newline character.   Is there a simple way to strip whitespace from $var (like trim() in )?   or   Is there a standard way of dealing with this issue?   I could use  or , but I'd like to think there is a more elegant solution to this problem.
45364
I have seen the following technique used many times on many different shells, to test if a variable is empty:  if [ "x$1" = "x" ]; then      # Variable is empty fi   Are there any advantages on using this over the more canonical if [ -z "$1" ]? Could it be a portability issue?
8853
Could you explain the following sentences from Bash manual about $_, especially the parts in bold,  maybe with some examples?      At shell startup, set to the absolute pathname used to   invoke the shell or shell script being executed as passed in the environment   or argument list.      Subsequently, expands to the last argument to the previous   command, after expansion.      Also set to the full pathname used to invoke each   command executed and placed in the environment exported to that command.     When checking mail, this parameter holds the name of the mail file.
91295
Why doesn't echo $1 print $1 in this simple bash script?    #!/bin/bash # function.sh print_something () { echo $1 } print_something  $ ./function.sh 123  -&gt; why doesn't it print '123' as a result?
45221
&gt; echo "hi" hi &gt; VAR='echo "hi"' &gt; $VAR "hi"   Why is the output of the above commands different?  A similar thing occurs with single quotes:  &gt; VAR="echo 'hi'" &gt; $VAR &gt; 'hi'
113935
I'm trying to write a function to replace the functionality of the exit builtin to prevent myself from exiting the terminal.  I have attempted to use the SHLVL environment variable but it doesn't seem to change within subshells:  $ echo $SHLVL 1 $ ( echo $SHLVL ) 1 $ bash -c 'echo $SHLVL' 2   My function is as follows:  exit () {     if [[ $SHLVL -eq 1 ]]; then         printf '%s\n' "Nice try!" &gt;&amp;2     else         command exit     fi }     This won't allow me to use exit within subshells though:  $ exit Nice try! $ (exit) Nice try!   What is a good method to detect whether or not I am in a subshell?
66174
This  gave me cd - as shorthand to change to my previous directory in Bash. It does have a major problem: it prints the directory it changes to, wasting my screen space, as that directory is then repeated in the prompt on the next line.  I know I can do cd $OLDPWD, also mentioned in that answer, and cd - &gt;/dev/null but that is much more typing.   In the bash man page it says:     ... or if - is the first argument, and the directory change is successful, the absolute path name of the  new  working  directory  is written to the standard output.   How can I make - not to be the first argument? By using any of the cd optional arguments? Any other way to suppress the echo or minimise typing without the need for defining functions/aliases on every machine I work with?
87437
I'm trying to beat my head against the syntax of this. I'm getting problems with whitespaces and I can't see if there's a better way to do this.  vardir=/home/user cd $vardir for i in $(ls "$vardir") ; do timestamp="$(stat -c %Y $i)" echo "$i" echo "$timestamp"  done   Result is similar no matter where I quote and I'm not sure what I'm doing wrong:  stat: cannot stat ‘Temporary’: No such file or directory Temporary  stat: cannot stat ‘Items’: No such file or directory Items   My goal is I want to grab the timestamps off of backup directories and touch the current ones to restore the original modified dates.
112122
Can anybody explain to me what the shell does in the two examples A) and B) below? It obviously behaves differently, but I can't find out why the output is different.  Example: Let's have a script in our current directory named bla.sh with only one command: echo ${0##/*} hello  A) Started as: ./bla.sh gives: ./bla.sh hello  B) Started as: . bla.sh gives: -bash hello  Since I use this in a script, the second output (because of the "-" in front of the -bash) kills the command. Of course, a simple -- before the ${...} helped, but I would love to understand what causes the output in the first place.I love bash. And vi[m]. But I digress…
30221
Suppose I want a bash command to do something extra.  As a simple example, imagine I just want it to echo "123" before running.  One simply way to do this would be to alias the command.  Since we still need the original, we can just refer to it by its exact path, which we can find using which.  For example:  $ which rm /bin/rm $ echo "alias rm='echo 123 &amp;&amp; /bin/rm'" &gt;&gt; .bashrc   This was easy because I was able to look up the path to rm using which.  However, I am trying to do this with exit, and which doesn't seem to know anything about it.  $ which exit $ echo $? 1   The command did not output a path, and in fact it returned a non-zero exit code, which which does when a command is not in $PATH.  I thought maybe it's a function, but apparently that's not the case either:  $ typeset -F | grep exit $ echo $? 1   So the exit command is not defined anywhere as a function or as a command in $PATH, and yet, when I type exit, it closes the terminal.  So it clearly is defined somewhere but I can't figure out where.  Where is it defined, and how can I call to it explicitly?
109504
I want to get vsftpd version into shell variable. I can get it to console with ease:  # vsftpd -version vsftpd: version 2.2.2   Also I can get a lot of other info into variable:  # i=`bash --version 2&gt;&amp;1 | head -n1`; echo "=$i="; =GNU bash, version 4.1.2(1)-release (x86_64-redhat-linux-gnu)=   (please note output is between "=" signs). This simple way does not work with vsftpd:  # i=`vsftpd -version 2&gt;&amp;1`; echo "=$i="; vsftpd: version 2.2.2 ==   Please note $i is "" here.  What am I doing wrong?
32835
Here is what I tried doing:  #!/bin/sh res=`df | awk 'FNR == 2 { print $5 }'` res2=$((res+0))   Here is my error:     -bash: 53%: syntax error: operand expected (error token is "%")   I Googled 'string to int bash' which showed me $((res+0)) but it doesn't work due to the % in the string (53%). How do I convert it in such a way I can do an if statement such as the one below (but that works)?  if [ "$res2" -ge 50 ] then     echo "Your disk &gt;=50% full!" fi