问题
I'm on Mac OSX and trying to put some basic aliases in .bashrc (e.g. alias ll = 'ls -l'
). I sourced .bashrc in .bash_profile, and on startup it recognizes a function that I have in .bashrc. However, I get the following error messages every time I add an alias and then try to start it up:
-bash: alias: ll: not found
-bash: alias: =: not found
-bash: alias: ls -l: not found
The ll alias does not work, but the command declared by the following function does:
#!/bin/bash
# prints the input
function print_my_input() {
echo 'Your input: ' $1
}
Is there an additional step I need to do to create normal aliases?
回答1:
Try this instead:
alias ls='ls --color=auto'
alias ll='ls -l'
alias la='ls -la'
alias cd..='cd ..'
alias ..='cd ..'
Bash does not allow a space before and after an =
sign when assigning variables as well as aliases.
On a sidenote, there are two ways to declare a function:
Using the keyword function
to indicate a function declaration
function myfunction { # function is a keyword
echo hello
}
or by simply putting braces after the function name and omitting the function
keyword
myfunction() { # () indicate a function definition
echo hello
}
Using both is not an error but redundant. Furthermore, Charles Duffy points out in the comments:
...not just redundant, but also needlessly nonportable. myfunction() { is guaranteed to work on all POSIX shells; function myfunction { works on old ksh (and is supported in bash for compatibility with same); combining the two doesn't work on baseline-POSIX or on old ksh.
来源:https://stackoverflow.com/questions/53900891/alias-not-found-and-alias-not-defined-with-alias-ll-ls-l-in-bash