Shell Scripting – Redirections
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed conditional statements in the shell.
https://cloudaffaire.com/shell-scripting-conditions/
In this blog post, we will discuss redirections in shell. Redirection simply means capturing output from a file, command, program, script, or even code block within a script and sending it as input to another file, command, program, or script. Each open file gets assigned a file descriptor. A file descriptor (FD) is a number which refers to an open file. Each open file gets assigned a file descriptor. When bash starts it opens the three standard file descriptors Standard Input (stdin with FD 0), Standard Output (stdout with FD 1), and Standard Error (stderr with FD 2).
Shell Scripting – Redirections:
Output Redirection:
Output of a file can be redirected using > which causes the file to be opened for writing on file descriptor n, or the standard output (FD 1) if n is not specified. If the file does not exist it is created; if it does exist it is truncated to zero size. If you want to append the output to existing file instead of overwriting you can use >>.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
####################################### ## Shell Scripting | IO Redirections ## ####################################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## ---------------------- ## Output Redirection (>) ## ---------------------- ## Redirect stdout to a file. echo "hello world" > myfile ## Create myfile if does not exist cat myfile echo "hello again" > myfile ## Overwrites myfile cat myfile echo "welcome" >> myfile ## Appends to existing content of myfile cat myfile echo "new file" >> myfile1 ## Create myfile1 if does not exist or append if exist cat myfile1 : > myfile ## truncates myfile cat myfile > myfile1 ## truncates myfile1 cat myfile1 |
Standard Output & Standard Error Redirection:
You can redirect Standard Output (stdout with FD 1) using 1> and Standard Error (stderr with FD 2) using 2>. If you want to redirect both Standard Output (stdout with FD 1) and Standard Error (stderr with FD 2) in the same place you can use 2>&1 or 1>&2.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
## ------------------------------------------ ## Standard output/Standard Error redirection ## ------------------------------------------ ## redirect with stdout and stderr file descriptor mkdir -p mydir/mydir1 ls mydir nodir ## list the content of mydir and returns error for nodir as it does not exist ls mydir nodir > myfile ## Redirects Standard output (stdout) to file and Standard Error (stderr) to Standard output (stdout) cat myfile ## returns the output of ls mydir ls mydir nodir 1> myfile ## Redirects Standard output (stdout) to file and Standard Error (stderr) to Standard output (stdout) cat myfile ## returns the output of ls mydir ls mydir nodir 2> myfile ## Redirects Standard Error (stderr) to file and Standard output (stdout) to Standard output (stdout) cat myfile ## returns the output of ls nodir ls mydir nodir > myfile 2>&1 ## Redirects both Standard output (stdout) and Standard Error (stderr) to file cat myfile ## returns the output of ls mydir nodir ls mydir nodir > myfile 1>&2 ## Redirects both Standard output (stdout) and Standard Error (stderr) to Standard output (stdout) cat myfile ## returns nothing |
Input Redirection:
Input to a file can be redirected using < which causes the file to be opened for reading on file descriptor n, or the standard input (FD 0) if n is not specified. Input redirection can also be achieved using heredoc (<<) and herestring (<<<).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
## ----------------- ## Input Redirection ## ----------------- ## redirect command input from file echo -e "1: one\n3: three\n4: four\n2: two" > myfile grep three < myfile ## content of myfile is redirected to grep sort < myfile ## content of myfile is redirected to sort cat < myfile ## content of myfile is redirected to cat ## input redirection with heredoc cat << EOF Hello world welcome to cloudaffaire EOF ## also supports variable substitution cat << EOF Hello $USER welcome to cloudaffaire EOF ## output of heredoc is redirected to myfile cat << EOF > myfile Hello $USER welcome to cloudaffaire EOF cat myfile ## supress variables substitution, 'EOF' can be used as well cat << "EOF" > myfile Hello $USER welcome to cloudaffaire EOF cat myfile ## input redirection with herestring grep world <<< "hello world" |
IO Redirection Using Pipe:
You can use a pipe to pass the output of one command as the input to another command. The pipe can also be combined with the IO redirection.
1 2 3 4 5 6 7 8 9 10 |
## ------------------------- ## IO Redirection Using Pipe ## ------------------------- ## using pipes (|), output of one command as input to another command cat /etc/passwd | head -n 7 | tail -n 5 cat /etc/passwd | head -n 7 | tail -n 5 > myfile cat myfile rm -r my* |
Hope you have enjoyed this article, in the next blog post, we will discuss array in the shell.