Echo is one of the basic tools that are most commonly used in bash scripting: it prints out a message to the user. This very message is printed using echo.
The usage of this command is very simple:
echo Words to print.
"
press
echo"If your phrase include newlines or special characters such as ', ;, :, &, !, and so on, you should enclose everything in quotes, like this:
echo\"This is something,
this is something else;
and this is just a random set of special chars:
&@%# :)\"
"
press
echo"If your phrase also includes one of the following special characters, apart from being quoted they also have to be escaped using a backslash (\\):
\\\"
\\'if everything is enclosed in single quotes
\\\$if followed by a letter or number
\\\`
Example:
echo This will print \\\"echo\\\` it works! \\\`\\\\\\\"
Will print:
`echo This will print \\\"echo\\\` it works! \\\`\\\\\\\"`
"
press
echo'If your phrase also includes a ! followed by a letter or number you should enclose the enclose the phrase in single quotes (\').'
press
echo"To create a file using echo the usage is also very simple:
echo\"This and
this text will go to a text file.\" > file.txt
This will recreate the file.
To append the text, insert two >> instead of 1 >
Of course, you can easily change the extension from txt to sh to create a shell script (just don't forget the shebang).
"
press
}
learn_1(){
echo"The shell is a command interpreter.
More than just the insulating layer between
the operating system kernel and the user, it's also
a fairly powerful programming language.
A shell program, called a script, is an easy-to-use
tool for building applications by \"gluing together\"
system calls, tools, utilities, and compiled binaries.
Virtually the entire repertoire of UNIX commands,
utilities, and tools is available
for invocation by a shell script.
If that were not enough, internal shell commands,
such as testing and loop constructs,
lend additional power and flexibility to scripts.
Shell scripts are especially well suited for
administrative system tasks and other routine
repetitive tasks not requiring the bells and
whistles of a full-blown tightly structured
programming language."
press
}
learn_2(){
exportcurrent="learn_2_lesson"
set +H
learn_2_lesson
until["$try"="y"];do
["$try"="n"]&&echo"Try again (I know you can do it!)!"
echo"
Task:
Create a script named first_script.sh that prints
\"Hello World! :)\"
(with the quotes) when you execute it. Use vi, nano or echo, typeexit when you're done,
r to repeat the lesson and s to skip the exercise."