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

using set -o in a bash script to set command line editing

我喜欢使用 vi 命令行编辑器,但是 bash_rc 和 bash_profile 属于 root。所以我所做的是创建一个脚本,我可以在多个终端上 运行 将命令行编辑器设置为 vi。然而,当我使用这个脚本时,它说是将 vi 设置为打开,但是在 运行 脚本之后,vi 仍然设置为关闭。

我不明白。

#!/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

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

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