What escape character will add a vertical tab to the screen if issued within a script?
echo is a shell built-in command that is used to print the information/message to your terminal. It is the most popular command that is available in most Linux distributions and is typically used in bash scripts and batch files to print status text/string to the screen or a file. Show
In this article, I will show you how to use the echo command in Linux shell scripts. How to Work With echo Command in LinuxWhen learning shell scripting this is the first command you will learn about to print something in your terminal. echo will print the output to stdout or you can also redirect the output to files. There are two versions of echo. One is bash builtin and the second one is an external command. NOTE: Always builtin version takes precedence over external command. Use the type command to get the path information about the echo command. $ type -a echo echo is a shell builtin echo is /usr/bin/echo echo is /bin/echo To get the list of options supported for the echo command, use the help option. $ help echo Example 1: No Arguments to echo CommandWhen you call the echo command without passing any arguments it prints an empty line. $ echoan echo with no arguments Example 2: Use echo With and Without QuotesYou can pass arguments to echo command with or without quotes. Take a look at the below example. I am printing the same statement with single and double quotes and without quotes and getting the same result. $ echo Love Linux And OpenSource $ echo "Love Linux And OpenSource" $ echo 'Love Linux And OpenSource'echo print output There is a significant difference in when to use single and double quotes. When you use single quotes and any word contains a single quote then it will be treated as the end of quotes. Take a look at the below example where isn’t contains single quotes and is treated as the end of quotes. $ echo 'Windows isn't opensource' > ^C In this case, using double quotes will make more sense. $ echo "Windows isn't opensource" Windows isn't opensourceEcho Single Vs Double Quotes Example 3: Printing Variables with Echo CommandValues stored in variables can be printed to the terminal using the echo command. When you use single quotes variable $ VAR="Welcome" $ echo ${VAR} to shell tips $ echo "${VAR} to shell tips" $ echo '${VAR} to shell tips'Echo Printing Variable Value Example 4: Redirecting Output to FileYou can redirect output to a file instead of printing it in a terminal using the redirection operator ( $ echo " Writing to file redirect.txt " > redirect.txt $ echo " Appending to file redirect.txt " >> redirect.txt
Example 5: Supported Argumentsecho command supports three arguments. Echo ArgumentsBy default when you run the echo command new line
character is automatically appended at the end. If you want to suppress this behavior use $ echo -n "Love Linux And OpenSource"Suppress New Line Character By using the $ echo "\nLove Linux And OpenSource" $ echo -E "\nLove Linux And OpenSource"Escaped Characters To use backslash-escaped characters you have to use Example 6: Escaped Charactersecho command supports escape characters like newline, tab, vertical tab, and a few more. You can get the list of supported characters from the help command. $ help echoEscaped Characters Let’s see
frequently used escape characters in action. Note you have to use Newline character(\n) – This will be one of the commonly used escape characters. When you use $ echo -e "\nWelcome to \nLinuxShellTips" Welcome to LinuxShellTips Horizontal tab(\t) – To create horizontal tabs use $ echo -e "\tWelcome to \tLinuxShellTips" Welcome to LinuxShellTips Vertical tab(\v) – To create vertical tabs use $ echo -e "\vWelcome \vto \vLinuxShellTips" Welcome to LinuxShellTips Carriage Return(\r) – Removes everything that comes before $ echo -e "Welcome to \r LinuxShellTips" LinuxShellTips Suppress Further Output(\c) – Carriage return removes everything that comes before it and $ echo -e "Welcome to \c LinuxShellTips" Welcome to If you want to treat any escape characters as normal characters you can use echo -e "Welcome to \\\c LinuxShellTips" Welcome to \c LinuxShellTips That’s it for this article. The echo is a simple command to use and very easy to learn. We will catch you up with more shell script-related articles soon. Which command can be issued to view all users currently logged on to a server and their tasks?w command is used to show logged-in user names and what they are doing. The information will be read from /var/run/utmp file. The output of the w command contains the following columns: Name of the user.
Which command will display the current working directory onscreen?To display the location of your current working directory, enter the command pwd.
Which keystroke combination should be used to access the tty3 terminal?Ctrl+Alt+F3 will bring up the login prompt of tty3.
What directory on a Linux system is commonly used to store log files and spools?CIT222 Chapter 4- Linux Filesystem Management Key Terms. |