-2

I have written this simple type alias script in my bin directory in home

#!/bin/sh
alias kp='ls –L'
alias ldir='ls –aF'
alias copy='cp'

and saved it in the name myenv. Then I have changed the mode using

chmod +x /bin/myenv

then execute it using the command

myenv

but after this when I use kp it says kp command not found. Why?

Eliah Kagan
  • 117,780

2 Answers2

2

Your script runs in a sub-shell by default. (It opens a new shell and runs your script. After the script has finished running, its modified environment is destroyed.)

If you'd like to change your current shell environment settings you have to:

source myenv

or:

. myenv

See man bash Shell Builtin Commands / Source.

lgarzo
  • 19,832
0

First, because character that you used in alias kp='ls –L' and alias ldir='ls –aF' is not the same with - (you can see that is a little bit longer). Try:

#!/bin/sh
alias kp='ls -L'
alias ldir='ls -aF'
alias copy='cp'

Just copy and paste from above.

Second, if you want that the script to have the expected effect, just put this line in ~/.bashrc file:

source /bin/myenv

Anyway, the best way to create aliases in Ubuntu is to use this method: https://askubuntu.com/a/5278/147044.

Radu Rădeanu
  • 169,590