-1

The below command works in command line

rsync -avh -r /Source/09_03_2016/ /Destination/
echo $(basename !:3)

Output 09_03_2016

But when I do this in shell script it does not work

#! /bin/bash

/usr/bin/rsync -avh -r /Source/09_03_2016 /Destination/;
echo $(basename !:3)

Output !:3

  • Are u sure u are using bash shell? type command ps $$ tell output.. – TKHN Oct 7 '16 at 9:34
  • Output :PID TTY STAT TIME COMMAND 6169 pts/0 Ss 0:00 -bash – HDev007 Oct 7 '16 at 9:35
  • Why did you add a ; at the end of the rsync command line in the script? – Kusalananda Oct 7 '16 at 9:46
  • @Kusalananda removed and tested still the same output – HDev007 Oct 7 '16 at 9:50
  • What is the meaning of "!:3" in echo command ? – GC 13 Oct 7 '16 at 9:57
2

Use the following directives in your script:

set -o history
set -o histexpand

This will enable the history expansion which is only enabled for interactive shells by default.

See man bash section HISTORY EXPANSION and SHELL BUILTIN COMMANDS command set for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.