【问题标题】:bash: if export PROMPT_COMMAND then remove rm $( ls -t | head -1) is not workingbash:如果 export PROMPT_COMMAND 然后删除 rm $( ls -t | head -1) 不起作用
【发布时间】:2016-08-18 17:19:28
【问题描述】:

下面的函数name 将使用函数myfunc 将当前工作目录名称设置为选项卡名称,但如果我将参数传递给name,那么它会将选项卡名称设置为传递的参数。
例如
name mytab [----> 这会将当前选项卡名称设置为 mytab]

我的 bashrc 如下:

function myfunc {  
echo -n -e "\033]0;${PWD##*/}\007" 
#--- some other thing ---
} 
function name {  
    if [ "$1" ]  
    then  
        unset PROMPT_COMMAND  
        echo -ne "\033]0;${*}\007"  
    else  
        unset PROMPT_COMMAND  
        export PROMPT_COMMAND="history -n; history -w; history -c; history -r; myfunc;$PROMPT_COMMAND"  
    fi  
}  
name  

所以如果我执行rm $(ls -t | head -1)
我收到错误/bin/rm: cannot lstat `\033[0m\033[0mReadme.txt\033[0m': No such file or directory

【问题讨论】:

  • 你能显示which ls的输出吗
  • which ls >> alias ls='ls --color' /bin/ls
  • 你可以测试命令rm $(ls --color=no -t | head -1)
  • 是的。有效。谢谢。

标签: bash shell


【解决方案1】:

请使用以下命令

rm $(ls --color=no -t | head -1)

这会输出ls 的结果,其中没有颜色,这就是使您的Readme.txt 显示为\033[0m\033[0mReadme.txt\033[0m 的原因。

或者,您可以通过说\ls 来绕过ls 的别名 --> \ls -t | head -1。更多信息在Why start a shell command with a backslash?。

【讨论】:

  • 是的,工作 gr8。我刚改成rm $(/bin/ls -t | head -1)
猜你喜欢
  • 1970-01-01
  • 2022-10-24
  • 1970-01-01
  • 2012-02-05
  • 2020-03-12
  • 2011-08-18
  • 2018-04-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多