3

I have add alias ll="ls -al" in .bash_profile, and when I input it in terminal,

~ $ ll

total 5080
drwxr-xr-x+  79 yanss  staff     2528 Sep  7 10:31 .
drwxr-xr-x    6 root   admin      192 Feb 14  2018 ..
-r--------    1 yanss  staff        7 May 15 19:45 .CFUserTextEncoding
-rw-r--r--@   1 yanss  staff    18436 Sep  6 17:26 .DS_Store
drwxr-xr-x    8 yanss  staff      256 Jun 20 09:51 .ShadowsocksX-NG
drwx------   19 yanss  staff      608 Sep  7 09:35 .Trash
drwxr-xr-x    3 yanss  staff       96 Jul  1  2017 .anaconda
...................

and then I execute this

exec $SHELL

it output nothing, but when I input ll again, it make an error

~ $ ll
bash: ll: command not found

but ls -al is still useful,

~ $ ls -al
total 5072
drwxr-xr-x+  80 yanss  staff     2560 Sep  7 10:39 .
drwxr-xr-x    6 root   admin      192 Feb 14  2018 ..
-r--------    1 yanss  staff        7 May 15 19:45 .CFUserTextEncoding
-rw-r--r--@   1 yanss  staff    18436 Sep  6 17:26 .DS_Store
.................
yanss
  • 126
  • 1
  • 6
  • 1
    `exec` replaces the current shell with a new program. Thus, `exec $SHELL` replaces the shell with a completely new instance of the program specified in the variable named `SHELL`. And if you just run `bash`, not `bash -l`, it doesn't run your `.bash_profile`, because `.bash_profile` is only run for *login* shells. – Charles Duffy Sep 07 '18 at 02:53
  • 1
    @CharlesDuffy thank u a lot, i find that, when i type `exec bash -l`, it becomes a login session, and `ll` is useful as before – yanss Sep 07 '18 at 03:39
  • BTW, it's generally more appropriate to put aliases in `.bashrc` than `.bash_profile`. `.bash_profile` is intended for things like environment variables and exported functions that are inherited by child processes, and in most cases should be written to call `.bashrc` when it's done (for things that *can't* be inherited). – Charles Duffy Sep 07 '18 at 15:04
  • Or you could make an exported function for `ll` instead of an alias (`unalias` it before doing the below, or operate in a new shell which doesn't have the alias set), which will be inherited by child processes without them needing to do anything special: `ll() { ls -l "$@"; }; export -f ll` – Charles Duffy Sep 07 '18 at 15:05

0 Answers0