#!/bin/bash
alias cp='cp -i'
alias # this line output: "alias cp='cp -i'"
touch /tmp/{a,b}.txt
cp /tmp/a.txt /tmp/b.txt # this line doesn't work as my expectation
I expect it will show prompt at end, like this: "cp: overwrite ‘/tmp/b.txt’?"
CodePudding user response:
It is because bash script is run in non-interactive mode. Try:
#!/bin/bash -i
or
shopt -s expand_aliases
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt.
See asnwers to: How to run an alias in a shell script?
Alternatively, you can use shell functions instead of aliases. Check answer to: In Bash, when to alias, when to script, and when to write a function?