Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.