-2

In a bash shell, why can't I create an alias

$ alias fooo="echo bac"
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

$ alias fooo='echo bac'
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'

$ fooo
fooo: command not found

$ alias fooo
bash: alias: fooo: not found

In another bash shell, the above commands succeed in creating an alias

$ alias fooo="echo bac"
$ fooo
bac

In the first shell, if I start a new shell (just type bash and press enter), or start a new login shell (type bash -l), the above commands also succeed as in the second shell.


Regarding the reply on alias command in the first shell

$ which alias
$ whereis alias
alias:
$ builtin alias fooo="echo bac"
alias alert='notify-send --urgency=low -i "$([ $? = 0 ] && echo terminal || echo error)" "$(history|tail -n1|sed -e '\''s/^\s*[0-9]\+\s*//;s/[;&|]\s*alert$//'\'')"'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias ll='ls -alF'
alias ls='ls --color=auto'


$ type alias
alias is a shell builtin
$ type -a alias
alias is a shell builtin

$ unalias alias
bash: unalias: alias: not found

Regarding the comment on echo in the first shell

$ echo hello
hello

$ whereis echo
echo: /bin/echo /usr/share/man/man1/echo.1.gz

$ which echo
/bin/echo
Tim
  • 101,790
  • In that first shell, does it work if you try to create that alias alone? – Julie Pelletier Dec 18 '16 at 00:19
  • @Theophrastus: Good observation. I thought it might be linked to a bad line separator but I reproduced your supposition and it behaves just like in the question. – Julie Pelletier Dec 18 '16 at 00:33
  • @Theophrastus see my update about echo. Is it disabled? – Tim Dec 18 '16 at 00:41
  • @JuliePelletier What did you mean by a bad line separator, and how did you reproduce his supposition? – Tim Dec 18 '16 at 00:43
  • Looking at your first shell, it appeared to be a paste of many alias calls but maybe @jlliagre got it right in their answer as it could be caused by a bad alias on alias itself. – Julie Pelletier Dec 18 '16 at 03:19
  • To reproduce Theophrastus' idea: alias echo=''; alias fooo='echo bac'; fooo – Julie Pelletier Dec 18 '16 at 03:20
  • Try replacing echo with printf to see if it does the same thing. – Julie Pelletier Dec 18 '16 at 04:56
  • Please write type -a alias and post output here. As there might be an alias on the word alias, also do unalias alias. Post results here. Then, after trying all that. Are you typing only alias fooo="echo bac" and getting all the output you posted?. What happens if you start a new shell in that console (just type bash and press enter), do you still get the same output? What is you start a new login shell (type bash -l)? Still in trouble?. –  Dec 18 '16 at 05:02
  • see my update. @sorontar – Tim Dec 18 '16 at 05:24
  • There are still other three questions: What are you typing (and what is output), start a new bash (exit will return to the present one), start a new login shell (also: exit will return to the running shell). –  Dec 18 '16 at 05:30
  • @sorontar sorry about confusion, see http://unix.stackexchange.com/posts/331144/revisions. I also ask how I can get the command history of the current shell only, so that I can look through it http://unix.stackexchange.com/questions/331170/how-to-get-the-command-history-only-for-the-current-shell-process – Tim Dec 18 '16 at 05:31
  • 1
    Two more questions: what is the output of alias fooo? and: What is the output of alias fooo='echo bac'? –  Dec 18 '16 at 05:34
  • I understand that if you type bash the command success, but: could you do alias fooo='echo bac' in that new shell successfully? –  Dec 18 '16 at 05:37
  • No, you have answered the second question with double quotes, not single quotes. Have you tried with single quotes? is the reference to that in your question? –  Dec 18 '16 at 05:39
  • @sorontar single quote also doesn't work. – Tim Dec 18 '16 at 05:40
  • Ok, so it is a temporal problem of just the shell that is running. A new shell clear up the problem. Still, and again, are you typing only alias fooo='echo bac' or the whole list of all the aliases? –  Dec 18 '16 at 05:42
  • @sorontar I typed alias fooo='echo bac', but not alias -p or alias. – Tim Dec 18 '16 at 05:50
  • And from you typing only alias fooo='echo bac' and pressing enter, you get the long list of aliases (which also go away with a new shell). If that is the correct description of what you see, I can not explain it. I'll suggest to you to assume that that is an unreproducible problem (it doesn't happen here to me nor (it seems) to the other guys trying to help you) and so start a new shell and move on to other issues/tasks/problems. I must give up: can't help. –  Dec 18 '16 at 05:58

2 Answers2

4

On the first shell, when you tried to define an alias you get an output with the existing aliases. This is wrong, you should have no output as on the second shell. I have reproduced the same problem if I define an alias name called 'alias'.

Try to find out what is actually executed, maybe just execute: builtin alias or builtin alias foo="echo bar" to force alias command to be used.

czvtools
  • 191
  • Thanks. about the command alias, see my update. – Tim Dec 18 '16 at 01:52
  • 1
    Very interesting, I'm curious what will be the reason.Did you try checking the environment settings (set or env) for any hint about alias. Maybe compare the environment between the problematic shell and the working sub-shell on the same host. – czvtools Dec 18 '16 at 18:42
1

You have a function defined that way in the first shell:

alias(){ builtin alias ; }

type alias should confirm that hypothesis.

jlliagre
  • 61,204