Alias replaces one string with another string. It's a simple replacement before processing the line further. If the alias is
alias ls='ls -lh'
then ls
at the beginning of the line will be replaced by ls -lh
. Period.
If ls
itself provided some syntax for negating an option, so a later option could override what's in front, then you could do this. With some options you can, e.g.:
ls --color=yes --color=no
If ls
was aliased to ls --color=yes
and you typed ls --color=no
, then the actual command would include both options and the typed one would win, because this is how ls
parses and understands its options. But you cannot make an option like -l
just disappear. If the alias makes it appear then it's there.
Some tools may provide options that contradict each other and the last one wins; or the first one wins. Some tools may provide a way to negate almost any of their options (e.g. --foo
vs. --no-foo
; --bar
vs. --no-bar
). It's always up to the tool and there is no widespread convention, as far as I know.
You may not use alias:
\ls
command ls
/bin/ls
"ls"
'ls'
Each of these will not trigger the alias, so you can append your desired set of options.
In Bash you can expand the line before you hit Enter. The default binding is Ctrl+Alt+e. This makes the following possible:
- Type
ls
.
- Hit Ctrl+Alt+e, you will see your alias expanded.
- Edit the line and remove the unwanted option.
- Prepend
\
, so the alias will not be expanded again after the next step.
- Hit Enter to execute.
Other shells may provide similar functionality.
With the particular alias in question this is not very spectacular because you can type the desired command easily by hand. With a complex alias it may be very useful: you edit a bit instead of typing everything anew.
Thanks for your thorough explanation. I think I'll opt to type \ls when I don't want to display a long list. Losing the -h parameter as well is OK because a short list doesn't include file size anyway. – OMA – 2019-11-26T01:15:26.907