Linux shell programming : shell substitution
bogotobogo.com site search:
shell substitution
The shell substitutes an expression that contains one or more special characters.
The following code substitues the variable var with its value and "\n" with a new line:
#!/bin/bash var=1234 echo -e "The value of var is $var \n" echo -E "The value of var is $var \n" echo "The value of var is $var \n" echo -n "The value of var is $var \n"
Output:
k@laptop:~/SHELL$ ./s.sh The value of var is 1234 The value of var is 1234 \n The value of var is 1234 \n The value of var is 1234 \nk@laptop:~/SHELL$
- -e option enables interpretation of backslash escapes.
- -E option to disable interpretation of backslash escapes (default).
- -n option to disable insertion of new line.
Command substitution
When shell encounters a command, it substitutes the command with output from the command given as:
`command`
Sample code:
#!/bin/sh date=`date` echo "date : $date" users=`who | wc -l` echo "Users : $user" up=`date ; uptime` echo "Uptime is $up"
Output:
date : Sun Nov 23 14:05:32 PST 2014 Users : Uptime is Sun Nov 23 14:05:32 PST 2014 14:05:32 up 1 day, 17:02, 4 users, load average: 0.52, 0.44, 0.30
Variable substitution
Depending on the variable's state, behavior of variable substitution varies.
Case | Substitutione | Description |
---|---|---|
1 | ${v} | Substitue the value of v. |
2 | ${v:-w} | If v is null or unset, w is substituted for v. The value of v does not change. |
3 | ${v:=w} | If v is null or unset, v is set to the value of w. |
4 | ${v:+w} | If v is set, w is substituted for v. The value of v does not change. |
5 | ${v:?w} | If v is null or unset, w is printed to standard error. This checks that variables are set correctly. |
#!/bin/sh echo 'Case 1' v1=1 echo ${v1} echo echo 'Case 2' echo ${v2:-2} echo $v2 echo echo 'Case 3' echo ${v3:=3} echo $v3 echo echo 'Case41 - set' v41=4 echo ${v41:+4444} echo $v41 echo echo 'Case42 - unset' echo ${v42:+4444} echo $v42 echo echo 'Case51 - set' v51=5 echo ${v51:?55555} echo $v51 echo echo 'Case52 - unset' echo ${v52:?55555} echo $v52
Output:
Case 1 1 Case 2 2 Case 3 3 3 Case41 - set 4444 4 Case42 - unset Case51 - set 5 5 Case52 - unset ./v.sh: 30: ./v.sh: v52: 55555
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization