The
echo
command will print its arguments to the terminal. When calling your script withmyFile.txt
as the first argument, this line:echo "mv $1 ${G_DEST_DIR}"
will print exactly this (and it will not move the file):
mv myFile.txt /data/door1
The other line, not prefixed with
echo
:mv $1 ${G_DEST_DIR}
will actually run the
mv
command and move your file to the/data/door1
directory.
Script analysis
Let's analyse your script, line by line:
1 G_DEST_DIR="/data/door1"
2
3 echo "mv $1 ${G_DEST_DIR}"
4 #&
5 mv $1 ${G_DEST_DIR}
- Declare a variable named
G_DEST_DIR
and put the string/data/door1
into the variable. - (empty line)
- Just display a message to the user. The shell won't read the message; only the user (probably) will.
- An ampersand
&
after a command means that theecho
command will be executed in background. Your script will not wait for the command to end, it will move on to the next line. (EDIT: line commented out after mklement0's comment.) - Moves (
mv
command) the file given as the first argument ($1
) to the directory named/data/door1
.
Bonus tip
Putting echo
before a command is a quick and effective way to dry-run the given command, that is, seeing what would be executed without echo
.