EXAM OBJECTIVES COVERED
2.3 Given a scenario, create, modify, and redirect files.
Now that we have learned how to use various commands to manage our files, let's look at how we can process text files in various ways. An introduction to many of the command-line tools for text manipulation is important to those interested in growing in their knowledge and use of Linux.
The echo Command
The echo command is used to display a line of text that is passed as an argument. This is a built-in command that is mostly used in shell scripts and batch files to output status text to the screen or a file.
echo [ OPTIONS ] [string]
Echo has a few options, but usually it is simply used by itself.
pbmac@ubuntu $ echo "This is Linux" This is Linux pbmac@ubuntu $ echo -e "The \nIs \nLinux" The Is Linux pbmac@ubuntu $
The first line simply repeats what is given on the command line - it "echoes" the text.
The -e option allows the use of certain special linefeed characters. In this example we see the \n, which stand for a linefeed - this puts a newline where the \n is.
The printf Command
The printf command is very similar to echo. This command is exactly the same as the printf command in the C programming language. It is used at times in Linux shell scripts, but is a bit more complex. However printf does provide some great options for outputting information that needs formatting.
pbmac@ubuntu $ name='Pat'; printf "Hi %s\n" $name Hi Pat pbmac@ubuntu $
The example above might be a bit confusing since we have not talked about shell scripts. The variable "name" is assigned a value, then the printf interprets the %s as saying "look at the next variable ($name) and interpret it as a string" (the %s says string - a %d says a decimal integer, %f is a floating point value). There are some good references on the web if you want to learn more about this command.