Asked 13 years, 11 months ago
Viewed 633 times
I have a few aliases in .bash_aliases
. I defined c
alias as follows, but it does not work as it should:
...
alias cd='cd; ls -r --time=atime'
alias c='cd'
...
In .bashrc
there is a line:
alias ls='clear; ls --color=auto'
Commnand c
gives bad output now. It should give the same output as cd; clear; ls -r --time=atime --color=auto
.
Other problem: When I type cd dir
I should stay in dir
but I'm in $HOME
as a result.
How can I solve this and improve in defining aliases? Is .bash_aliases
interpreted as a regular grammar
?
67.5k9494 gold badges225225 silver badges295295 bronze badges
asked Sep 1, 2011 at 16:17
xralfxralf15.2k3131 gold badges113113 silver badges160160 bronze badges
2Use functions instead, the advantage being the possibility to pass parameters and a cleaner syntax.
function cd() {
command cd "$@"
ls -r --time=atime
}
function c() {
cd "$@"
}
function ls() {
clear
command ls --color=auto "$@"
}
(command
is a bash
builtin used to refer to the real command, and not functions with the same name).
answered Sep 1, 2011 at 16:42
enzotibenzotib53.2k1414 gold badges126126 silver badges106106 bronze badges
4c
should be exactly equivalent to cd
. I expect you'll see the same error from cd dir
as from c dir
, and that c
alone works.
cd
won't work the way you've defined it, because aliases perform a simple text substitution. cd dir
is expanded to cd; ls -r --time=atime dir
. Aliases are pretty much limited to giving a command a shorter name or providing default options, e.g. alias c=cd
or alias cp='cp -i'
. For anything more complex, such as running several commands, use a function.
cd () {
command cd "$@" &&
ls -r --time=atime
}
See also Aliases vs functions vs scripts, How to pass parameters to an alias?.
answered Sep 1, 2011 at 23:27
1 You must log in to answer this question.Start asking to get answers
Find the answer to your question by asking.
Ask questionExplore related questions
See similar questions with these tags.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4