linux - Bash 为带有反引号/可执行部分的命令设置别名(转义反引号)

标签 linux bash shell

我制作了一个简单的 bash 命令来清除 Laravel 中的缓存:

for w in `php artisan | grep 'clear' | awk '{print $1;}'`; do php artisan $w; done

我想将其添加为别名,例如我只是做laravel-cache。我跑了:

alias laravel-cache="for w in `php artisan | grep 'clear' | awk '{print $1;}'`; do php artisan $w; done"

认为这会添加一个别名,但是当运行 alias 来查看它们时,我发现:

# alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l='ls -CF'
alias la='ls -A'
alias laravel-cache='for w in clear-compiled
auth:clear-resets
cache:clear
config:clear
route:clear
view:clear; do php artisan view:clear; done'
alias ll='ls -alF'
alias ls='ls --color=auto

但显然这不起作用:

# laravel-cache
bash: syntax error near unexpected token `auth:clear-resets

所以,我的问题是如何转义反引号(或在运行别名时而不是在添加别名时执行的可执行部分)?

最佳答案

您可能应该创建一个函数而不是别名:

单行

laravel-cache () { for w in $(php artisan | grep 'clear' | awk '{print $1}'); do php artisan "$w"; done ; }

已格式化

laravel-cache () { 
  for w in $(php artisan | grep 'clear' | awk '{print $1}') 
    do
      php artisan "$w"
  done ;
}

通常,只要不只是更改命令的默认选项(例如通过管道传输到其他命令),建议使用函数。

例如,您可以将其添加到您的 ~/.bash_profile 中,然后像别名一样使用它。添加到您的个人资料后,只需获取它即可,您应该准备好使用以下命令:

$ source ~/.bash_profile

关于linux - Bash 为带有反引号/可执行部分的命令设置别名(转义反引号),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42997223/

相关文章:

c - linux内核 fork 子状态返回

java - java中如何检查shell脚本是否执行成功

python - 在 bash 中将数据粘贴在一起

c - 不同的pid cgi程序

linux - 将/dev/video0 的输出转储到 fifo 文件

linux - 别名 mkdir 以在 Mac 上打印详细输出

linux - 在bash中将字符串拆分为数组

python - 在单个端口上收集大型 ip hoSTList 的默认登录页面 URL

python - 如果值不存在,则从 python 向 key=value 添加值

linux - 在 bash 中的 if 子句中组合条件