A topic that is not much know by many people that use bash on Linux it’s the use of the special parameters available in bash.
These parameters may only be referenced; assignment to them is not allowed, i’m talking about the parameters like $$ or $?.
Let’s see some example with them.
$?
Expands to the exit status of the most recently executed foreground pipeline. By using this you can check whether your bash script is completed successfully or not, usually an exit status of 0 means that the command has terminated without error, while a number means a specific error.
Simple example, create a file and do a ls of it, these operation are successful so the exit code is 0, while a ls of a not existing file return 2:
(%:/tmp)┌- touch myfile (%:/tmp)┌- echo $? 0 (%:/tmp)┌- ls myfile myfile (%:/tmp)┌- echo $? 0 (%:/tmp)┌- ls not-myfile ls: cannot access not-myfile: No such file or directory (%:/tmp)┌- echo $? 2 |
Colour part of your prompt red to indicate an error.
If the return code from the last command was greater than zero, colour part of your prompt red. The commands give a prompt like this:
[user current_directory]$
After an error, the “[user” part is automatically coloured red.
Tested using bash on xterm and terminal. Place in your .bashrc or .bash_profile.
export PROMPT_COMMAND='if (($? > 0)); then echo -ne "\033[1;31m"; fi'; export PS1='[\[\]\u\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\]]\$ ' |
Cracking a password protected .rar file in a line, by dictionary attack method. All you need is a good dictionary (dict.txt).
Basically it cycles until the exit status it’s equal to 0
for i in $(cat dict.txt);do unrar e -p$i protected.rar; if [ $? = 0 ];then echo "Passwd Found: $i";break;fi;done |
$$
Expands to the process ID of the shell. In a () subshell, it expands to the process ID of the invoking shell, not the subshell.
Show the process ID of a terminal:
(%:/tmp)┌- echo $$ 3177 (%:/tmp)┌- ps -ef |grep 3177 linuxaria 3177 3124 0 12:29 pts/2 00:00:00 zsh linuxaria 6714 3177 0 22:39 pts/2 00:00:00 ps -ef linuxaria 6715 3177 0 22:39 pts/2 00:00:00 grep 3177 |
exit without saving history
this exits bash without saving the history. unlike explicitly disabling the history in some way, this works anywhere, and it works if you decide *after* issuing the command you don’t want logged, that you don’t want it logged
kill -9 $$ |
Figure out what shell you’re running
readlink -f /proc/$$/exe |
$# and $0
$# Expands to the number of positional parameters in decimal.
This is typically used to check at the start of a bash script if the user has typed the correct number of arguments, to verify that it has typed three:
#!/bin/bash if [ $# -lt 3 ] then echo "Usage: $0 arg1 arg2 arg3" exit 3 fi |
In this example i’ve used also the special parameter $0 that it’s expanded with the name of the shell or shell script
$@ and $*
$* Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, it expands to a single word with the value of each parameter separated by the first character of the IFS special variable. That is, “$*” is equivalent to “$1c$2c…”, where c is the first character of the value of the IFS variable. If IFS is unset, the parameters are separated by spaces. If IFS is null, the parameters are joined without intervening separators.
$@ Expands to the positional parameters, starting from one. When the expansion occurs within double quotes, each parameter expands to a separate word. That is, “$@” is equivalent to “$1” “$2” …. When there are no positional parameters, “$@” and $@ expand to nothing (i.e., they are removed).
As overview:
Syntax | Effective result |
---|---|
$* |
$1 $2 $3 ... ${N} |
$@ |
$1 $2 $3 ... ${N} |
"$*" |
"$1c$2c$3c...c${N}" |
"$@" |
"$1" "$2" "$3" ... "${N}" |
You see that without being quoted (double-quoted), both have the same effect: All positional parameters from $1
to the last used one are expanded without any specials.
When the $*
special parameter is doublequoted, it expands to the equivalent of: "$1c$2c$3c$4c........$N"
, where ‘c’ is the first character of IFS
.
But when the $@
special parameter is used inside doublequotes, it expands to the equivanent of…
"$1" "$2" "$3" "$4" ..... "$N"
…which exactly reflects all positional parameters like they were initially set and given to the script or the function. If you want to re-use your positional parameters to call another program (for example in a wrapper-script), then this is the choice for you, use the doublequoted "$@"
.
Bash that print information on the arguments given at the command line:
#!/bin/sh echo "I was called with $# parameters" echo "My name is $0" echo "My first parameter is $1" echo "My second parameter is $2" echo "All parameters are $@" |
These are the most common special parameters used in bash, there are some more that you can check at these sites:
Bash reference manual
The Geek stuff
Popular Posts:
- None Found