Demonstration here,
$ :
$ echo $?
0
- The colon is used in case of while loop to make endless iteration. Example of while endless loop is,
while :
do
...
done
which is same as,
while true
do
...
done
- Colon (:) can be used as placeholder in if/then test.
Example:
if condition
then : # Do nothing and branch ahead
else # Or else ...
take-some-action
fi
After "then" the colon (:) indicates do nothing if "if condition" is satisfied and ahead branch.
- In case of, "${parameter:-default}" colon (:) makes a difference only when the parameter has been declared, but is set to null. In this case it will echo default. Example.
$ cat >colon_test.sh
# Colon makes to trigger the default optuion
#+if the variable is declared and set to null/
username=
echo "username has been declared, but is set to null."
echo "username = ${username-`whoami`}"
# This will not echo as variable is set to null and is not used colon (:).
echo "username = ${username:-`whoami`}"
# Will echo the execution of `whoami` command as we used colon (:) here.
$ sh colon_test.sh
username has been declared, but is set to null.
username =
username = Arju
$ whoami
Arju
- In case of arithmetic operations, while assigning variable ":" is necessary because otherwise Bash attempts to interpret statement as a command.
Example:
$ cat >arithmetic_colon.sh
n=1; echo -n "$n "
: $((n = $n + 1))
# Here ":" is necessary. If we don't use colon then Bash attempts
#+ to interpret "$((n = $n + 1))" as a command.
echo -n "$n "
$ sh arithmetic_colon.sh
1 2
Note that we could also increment n by 1 using "(( n = n + 1 ))" or "n=$(($n + 1))".
- Colon (:) also can be used to evaluate environmental variable. Before doing any operation it can be checked whether certain environmental variable is set.
$ cat >check_environment.sh
# Check some of the system's environmental variables.
: ${HOSTNAME?} ${USER?}
$ sh check_environment.sh
$ cat >check_environment2.sh
# Check some of the system's environmental variables.
: ${HOSTNAME?} ${USER?} ${ORACLE_HOME?}
$ sh check_environment2.sh
check_environment2.sh: line 2: ${$ORACLRACLE_HOME?}: bad substitution
In the above example see if all environmental variables were set then no error returned but as ORACLE_HOME variable was not set and so it returned error.
- In combination with the redirection operator (>) colon, truncates a file to zero length, without changing its permissions. If the file did not previously exist, creates it.
For example
$ : >new_file.txt # File "new_file.txt" is now empty.
It have the similar effect as statement cat /dev/null >new_file.txt however, this does not fork a new process, since ":" is a builtin.
If : is used with >> redirection operator, then it has no effect on a pre-existing target file (: >> target_file). If the file did not previously exist, then it creates it.
- Colon (:) can be used to comment a line but not recommended. Because with : error checking is done in the line.
- The ":" also serves as a field separator, in /etc/passwd, and in the $PATH variable.
Example:
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin
Related Documents
No comments:
Post a Comment