5

I'm stuck with the following problem.

I've defined an alias in .bashrc (and .bash_profile):

alias echo2="echo"

This works well:

$ echo2 "test"
test

However, if I try to use it after a redirect, it doesn't work:

ls | awk '{print "echo2 "$1}' | bash
bash: line 1: echo2: command not found
bash: line 2: echo2: command not found
...

Does someone know why? How I could make it work?

1 Answer 1

7

You're piping it into a new bash process as input. That process doesn't load your initialization scripts though, so there's no alias defined.

Check the section INVOCATION in man bash: Depending on the file in which you defined the alias, you need to make the bash process either a login shell (-l) or interactive (-i) to load that file.

There's an additional restriction: Aliases are ignored when the shell isn't interactive. There's a workaround though:

   Aliases  are not expanded when the shell is not interactive, unless the
   expand_aliases shell option is set using shopt (see the description  of
   shopt under SHELL BUILTIN COMMANDS below).

Alternatively, you could look into using shell functions instead of aliases.

You must log in to answer this question.

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