“alias: =: not found”, and alias not defined, with “alias ll = 'ls -l'” in .bashrc

不打扰是莪最后的温柔 提交于 2021-02-04 21:52:00

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!