0

When I type echo "${PATH}" | tr -s ':' '\n' | nl from inside a bash script and at the terminal I get the same result:

     1  /home/nikhil/Documents/Git/Cs/Architecture/bin
     2  /home/nikhil/.local/bin
     3  /home/nikhil/opt/.nvm/versions/node/v16.13.0/bin
     4  /home/nikhil/opt/bin
     5  /usr/local/sbin
     6  /usr/local/bin
     7  /usr/sbin
     8  /usr/bin
     9  /sbin
    10  /bin
    11  /usr/games
    12  /usr/local/games
    13  /snap/bin
    14  /home/linuxbrew/.linuxbrew/bin
    15  /home/linuxbrew/.linuxbrew/sbin
    16  /home/nikhil/.cargo/bin
    17  /home/nikhil/.cabal/bin
    18  /home/nikhil/opt/go/bin
    19  /home/nikhil/.ruby/bin
    20  /home/linuxbrew/.linuxbrew/opt/fzf/bin

But when I type the following inside a bash script and on terminal I get different results:

# From Terminmal
$ type pandoc
pandoc is aliased to `/usr/bin/pandoc'
pandoc is /usr/bin/pandoc
pandoc is /home/linuxbrew/.linuxbrew/bin/pandoc
pandoc is /home/nikhil/.cabal/bin/pandoc
# From inside bash script
pandoc is /usr/bin/pandoc

Why does type have different output from inside the bashscript and from the terminal? How can I make the bash script type output to be the same as terminal's?

0

1 Answer 1

8

It looks like you have type aliased to type -a. Aliases are not inherited by any shell scripts you run from your terminal, and scripts are run in non-interactive mode by default.

Because scripts are run in a non-interactive shell, ~/.bashrc won't be sourced when bash runs the script, so aliases defined there won't be loaded.

Without -a, type will "indicate how it would be interpreted if used as a command name" - i.e. it will show you what would actually be run. With -a, it will show you all possible matches - executables in $PATH (both direct and via following symlinks), aliases, functions)

e.g. on my system, grep is aliased:

$ type grep
grep is aliased to `grep --directories=skip --binary-files=without-match'

$ type -a grep
grep is aliased to `grep --directories=skip --binary-files=without-match'
grep is /bin/grep

$ type -P grep
/bin/grep

My aliases aren't inherited if I run type in a (non-interactive) instance of bash:

$ bash -c 'type grep'
grep is /bin/grep

If I force bash to be run in interactive mode, it will source ~/.bashrc (which, in turn, sources my ~/.bash-aliases file).

$ bash -i -c 'type grep'
grep is aliased to `grep --directories=skip --binary-files=without-match'

NOTE: it's not a good idea to just make your scripts use bash -i as their interpreter. Instead, define any aliases or functions needed in your script in the script itself, or source them from another file. Or just use the command with whatever options are needed in the script - aliases are a convenience to minimise repetitive typing, which isn't really needed in a script. BTW, type's -P option is generally the most useful option in a script.

See help type:

type: type [-afptP] name [name ...]

Display information about command type.

For each NAME, indicate how it would be interpreted if used as a
command name.

Options:
  -a        display all locations containing an executable named NAME;
            includes aliases, builtins, and functions, if and only if
            the `-p` option is not also used

  -f        suppress shell function lookup

  -P        force a PATH search for each NAME, even if it is an alias,
            builtin, or function, and returns the name of the disk file
            that would be executed

  -p        returns either the name of the disk file that would be executed,
            or nothing if `type -t NAME` would not return `file`

  -t        output a single word which is one of `alias`, `keyword`,
            `function`, `builtin`, `file` or ``, if NAME is an alias,
            shell reserved word, shell function, shell builtin, disk file,
            or not found, respectively

Arguments:
  NAME      Command name to be interpreted.

Exit Status:
Returns success if all of the NAMEs are found; fails if any are not found.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .