Question

I am new to Unix and learning the walk and talk of it. Lately I came across this syntax in my script which made me confused between the two:

G_DEST_DIR="/data/door1"

echo "mv $1 ${G_DEST_DIR}"
#&
mv $1 ${G_DEST_DIR}

According to my intel the above code will move the file(which is passed as an argument) and move it to the dest_dir

My question is what is the difference between

echo "mv $1 ${G_DEST_DIR}"

and

mv $1 ${G_DEST_DIR}

If I want to move a file which is passed as an argument how can I do that

Was it helpful?

Solution

  • The echo command will print its arguments to the terminal. When calling your script with myFile.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}
  1. Declare a variable named G_DEST_DIR and put the string /data/door1 into the variable.
  2. (empty line)
  3. Just display a message to the user. The shell won't read the message; only the user (probably) will.
  4. An ampersand & after a command means that the echo 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.)
  5. 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.

OTHER TIPS

  • echo = displays/prints a line of text
  • mv = moves/renames the file

EDIT: Example: Let say $1 (first argument) is old.txt

  • echo "mv $1 ${G_DEST_DIR}" will print out "mv old.txt /data/door1"
  • mv $1 ${G_DEST_DIR} will move old.txt into directory /data/door1

echo does nothing put print the (one and only) argument to stdout - that its argument has the form of a shell command doesn't make a difference.

By contrast, the line that starts with mv performs an actual file move/rename command.

Prepending echo to a command is frequently used for testing to see what a command would do when executed, without actually executing (a potentially destructive/irreversible action).

As for the mv command: $1 references the first argument passed to the script; to be safe, you should double-quote the reference, as should be done for variable references in general ("$1" and "${G_DEST_DIR}").

echo command displays anything written b/w "",but if u use any command and it will get eexecuted (if valid )first and then its o/p will be displayed. so thats why your both command will move the data to file but in first case line with echo the statement get esecuted with move command but it havo no o/p so further it displays the mv then the filename as per given g_dest_dir variable.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top