Alias not assigned correctly

1

I have a weird issue with a bash alias. I have the following alias defined in my .bashrc:

alias rec='nano `ls slurm-* | sort -t. -k3n | tail -1`'

However, when I expand the alias rec in the shell with CTRL ALT E I get:

nano Remember that all constraints must be readded!

If I execute the alias, I get the same thing:

nano Remember that all constraints must be readded!

If I use which rec I get the correct expansion:

alias rec='nano `ls slurm-* | sort -t. -k3n | tail -1`'
/home/spack/opt/spack/linux-rhel6-x86_64/gcc-4.4.7/nano-2.6.3-k2cwz7lwjd4zutb7r7cfo63apevyztuw/bin/nano
/home/spack/opt/spack/linux-rhel6-x86_64/gcc-4.4.7/coreutils-8.26-icpocuezd6r7ydd73ipkehkg345372eo/bin/sort
/home/spack/opt/spack/linux-rhel6-x86_64/gcc-4.4.7/coreutils-8.26-icpocuezd6r7ydd73ipkehkg345372eo/bin/tail

This Remember that all constraints must be readded! is the output of a (supposedly) completely unrelated script that I have written. Any idea what's going on here?

captainalright

Posted 2017-08-12T22:33:49.857

Reputation: 11

Pretty much any time you try to do something complex with an alias and it isn't working out, you should make it a function instead--though here be careful about parsing the output of ls as well – Eric Renouf – 2017-08-13T00:14:33.547

Answers

1

You are parsing ls, you shouldn't do this. I think it may be the source of your problem.

E.g. if at lease one of your slurm-* filenames is like:

*\nRemember that all constraints must be readded!

or

*\nRemember that all constraints must be readded!\n*

where \n is a newline, then sort receives more lines than you expect. This filename alone:

slurm-0.1.-5\nRemember that all constraints must be readded!

will make sort receive two lines and then in its output the Remember … line will be after the slurm-… line. I don't know how your slurm-* filenames look like exactly. I'm not even sure this is what really happens. My point is you shouldn't parse the output of ls.


Another weak fragment is where you don't quote the argument for nano. This is probably unrelated to your current problem but keep in mind nano a b makes nano edit a, then b, not "a b".

Kamil Maciorowski

Posted 2017-08-12T22:33:49.857

Reputation: 38 429