Linux shell programming : metacharacters & quotes
The command options, option arguments and command arguments are separated by the space character. However, we can also use special characters called metacharacters
in a Linux command that the shell interprets rather than passing to the command.
The shell metacharacters
are listed here for reference:
Symbol | Meaning |
---|---|
> | Output redirection |
>> | Output redirection (append) |
< | Input redirection |
* | File substitution wildcard; zero or more characters |
? | File substitution wildcard; one character | [ ] | File substitution wildcard; any character between brackets |
`cmd` | Command Substitution |
$(cmd) | Command Substitution |
| | The Pipe (|) |
; | Command sequence, Sequences of Commands |
[ ] | File substitution wildcard; any character between brackets |
|| | OR conditional execution |
&& | AND conditional execution |
( ) | Group commands, Sequences of Commands |
& | Run command in the background, Background Processes |
# | Comment |
$ | Expand the value of a variable |
\ | Prevent or escape interpretation of the next character |
<< | Input redirection |
Linux for Programmers and Users, Section 5.16:
Sometimes we need to pass metacharacters
to the command being run and do not want the shell to interpret them. There are three options to avoid shell interpretation of metacharacters
.
- Escape the metacharacter with a backslash (\). (See also Escaped Charaters) Escaping characters can be inconvenient to use when the command line contains several metacharacters that need to be escaped.
- Use single quotes (' ') around a string. Single quotes protect all characters except the backslash (\).
- Use double quotes (" "). Double quotes protect all characters except the backslash (\), dollar sign ($) and grave accent (`). Double quotes is often the easiest to use because we often want environment variables to be expanded.
The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the shell:
#!/bin/sh echo "\$9,800"
Output:
$9,800
How we can print out the following?
echo <-$1500.**>; (update?) [y|n]
We can use Escape:
echo \<-\$1,234.\*\*\>\; \(update\?\) [y\|n]
But there is a simple solution:
echo '<-$1500.**>; (update?) [y|n]'
Any characters within single quotes are quoted just as if a backslash is in front of each character.
There are cases single quote cannot be a solution. For example:
#!/bin/sh balance=1234 echo 'My account balance is $balance; [as of (`date +%m/%d`)]'
But this produces:
My account balance is $balance; [as of (`date +%m/%d`)]
It is obvious that single quotes prevent variable substitution. If we want to substitute variable values and to make invert commas work as expected then we would need to put our commands in double quotes as follows:
#!/bin/sh balance=1234 echo "My account balance is \$$balance; [as of (`date +%m/%d`)]"
Now we have the output we want:
My account balance is $1234; [as of (11/23)]
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