在bash脚本中使用set -o来设置命令行编辑

问题描述 投票:0回答:1

我喜欢使用vi命令行编辑器,但是bash_rc和bash_profile由root拥有。所以做的是创建一个脚本,我可以在多个终端上运行,将命令行编辑器设置为vi。但是当我使用这个脚本时,它表示将vi设置为on,但是在运行脚本之后,vi仍然设置为off。

我不明白。

#!/bin/bash

check_set() {
chckifvi=$(set -o | grep "\bvi\b"| awk '{print $NF}')
}

check_set
echo "VIM command line editing is set to $chckifvi"
if [[ "$chckifvi" == "off" ]] ; then
set -o vi
        check_set
        echo "VIM Command line editing is set to $chckifvi"
    else
        echo "VIM Comamnd line editing already set to $chckifvi"
fi




casper@casperfi 1006$ ~/bin/editerSet.sh
VIM command line editing is set to off
VIM Command line editing is set to on
casper@casperfi 1007$ set -o
allexport       off
braceexpand     on
emacs           on
errexit         off
errtrace        off
functrace       off
hashall         on
histexpand      on
history         on
ignoreeof       off
interactive-comments    on
keyword         off
monitor         on
noclobber       off
noexec          off
noglob          off
nolog           off
notify          off
nounset         off
onecmd          off
physical        off
pipefail        off
posix           off
privileged      off
verbose         off
vi              off
xtrace          off
bash set vi
1个回答
3
投票

运行. ~/bin/editorSet.sh而不是~/bin/editorSet.sh,在您已运行的交互式shell中执行脚本命令。 (在bash中,但不是所有POSIX shell,你可以使用source作为.的同义词)。

否则,它将在脚本运行时退出的新shell中运行,因此配置更改不会持续超过脚本执行的结束。

© www.soinside.com 2019 - 2024. All rights reserved.