例如:
$ ls -aF
./ ../ bin/
$ cd tin # with a tee, not bee
bin
$ pwd
/home/user/bin
Run Code Online (Sandbox Code Playgroud)
换句话说,cd猜测我真正的意思是cd bin,并成功(嗯?)相应地更改当前目录。我没有在Bash 参考手册man bash中找到这种行为的记录。
我希望 Bash 产生一个错误,向标准错误写入一些信息,如果没有找到与参数匹配的目录(考虑扩展) ,则保持当前目录不变。dir
供参考,
$ type cd
cd is a shell builtin
$ ps -p $$
PID TTY TIME CMD
46959 pts/8 00:00:00 bash
$ bash --version
GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version …Run Code Online (Sandbox Code Playgroud) 如何让 bash 默认使用时间二进制文件 (/usr/bin/time) 而不是 shell 关键字?
which time返回/usr/bin/time
type time返回time is a shell keyword
运行time显然是在执行shell关键字:
$ time
real 0m0.000s
user 0m0.000s
sys 0m0.000s
$ /usr/bin/time
Usage: /usr/bin/time [-apvV] [-f format] [-o file] [--append] [--verbose]
[--portability] [--format=format] [--output=file] [--version]
[--quiet] [--help] command [arg...]
Run Code Online (Sandbox Code Playgroud)
enable -n time 返回 bash: enable: time: not a shell builtin
是否可以制作一个类似的功能
function doStuffAt {
cd $1
# do stuff
}
Run Code Online (Sandbox Code Playgroud)
但是让它如此调用该函数实际上并没有改变我的密码,它只是在函数的持续时间内改变它?我知道我可以保存密码并在最后设置它,但我希望有一种方法可以让它在本地发生而不必担心。
我输入help suspend并得到了这个简短的解释:
suspend: suspend [-f]
Suspend shell execution.
Suspend the execution of this shell until it receives a SIGCONT signal.
Unless forced, login shells cannot be suspended.
Options:
-f force the suspend, even if the shell is a login shell
Exit Status:
Returns success unless job control is not enabled or an error occurs.
Run Code Online (Sandbox Code Playgroud)
我的理解是:我输入suspend并且终端冻结,甚至 strg + c 也无法解冻它。但是,当我打开另一个终端并搜索冻结终端的 PID 并键入kill -SIGCONT PID-NR一个 SIGCONT 信号时,它会发送到冻结终端并将其解冻,使其解冻。
但是,暂停终端的实际目的是什么?哪些日常应用是它的典型应用?让它成为内置 shell 的人有什么想法?
从有关的printf是否是这个问题,一个内置的佳日,谈到这个答案是报价POSIX标准。
答案指出,POSIX 搜索顺序是查找所需命令的外部实现,然后,如果 shell 已将其实现为内置,则运行内置。(对于不是特殊内置函数的内置函数。)
为什么 POSIX 要求在允许运行内部实现之前存在外部实现?
似乎……随意,所以我很好奇。
在阅读了 ilkkachu 对这个问题的回答后,我了解到内置的declare(带参数-n)shell的存在。
help declare 带来:
设置变量值和属性。
声明变量并赋予它们属性。如果没有给出名称,则显示所有变量的属性和值。
-n ... 使 NAME 成为对其值命名的变量的引用
我要求用一个例子declare做一个一般性的解释,因为我不理解man. 我知道什么是变量和扩大,但我还是错过了man上declare(可变属性?)。
也许您想根据 ilkkachu 在答案中的代码来解释这一点:
#!/bin/bash
function read_and_verify {
read -p "Please enter value for '$1': " tmp1
read -p "Please repeat the value to verify: " tmp2
if [ "$tmp1" != "$tmp2" ]; then
echo "Values unmatched. Please try again."; return 2
else
declare -n ref="$1"
ref=$tmp1
fi
}
Run Code Online (Sandbox Code Playgroud) 我使用 Bash 作为我的交互式 shell,我想知道是否有一种简单的方法让 Bash 在它们共享相同名称的情况下运行系统命令而不是 shell 内置命令。
例如,使用系统kill(from util-linux) 打印指定进程的进程 ID (pid) 而不是发送信号:
$ /bin/kill -p httpd
2617
...
Run Code Online (Sandbox Code Playgroud)
在不指定系统命令的完整路径的情况下,使用 Bash 内置命令代替系统命令。该kill内建不具备-p这样的命令失败选项:
$ kill -p httpd
bash: kill: p: invalid signal specification
Run Code Online (Sandbox Code Playgroud)
我尝试了Make bash use external `time` command 而不是shell builtin 中列出的答案,但它们中的大多数只能工作,因为time实际上是 shell关键字- 而不是 shell builtin。
除了暂时禁用内置的 Bash 之外enable -n kill,到目前为止我见过的最好的解决方案是使用:
$(which kill) -p httpd
Run Code Online (Sandbox Code Playgroud)
是否有其他更简单(涉及较少输入)的方法来执行外部命令而不是内置的 shell?
请注意,这kill …
time写入stderr,因此人们会假设添加2>&1到命令行应该将其输出路由到stdout. 但这不起作用:
test@debian:~$ cat file
one two three four
test@debian:~$ time wc file > wc.out 2>&1
real 0m0.022s
user 0m0.000s
sys 0m0.000s
test@debian:~$ cat wc.out
1 4 19 file
Run Code Online (Sandbox Code Playgroud)
只有括号才有效:
test@debian:~$ (time wc file) > wc.out 2>&1
test@debian:~$ cat wc.out
1 4 19 file
real 0m0.005s
user 0m0.000s
sys 0m0.000s
Run Code Online (Sandbox Code Playgroud)
为什么在这种情况下需要括号?为什么不被time wc解释为一个单一的命令?
如果我想获得 bash 内置的简短使用消息,我可以help <builtin>在命令提示符下使用,例如
$ help export
export: export [-fn] [name[=value] ...] or export -p
Set export attribute for shell variables.
Marks each NAME for automatic export to the environment of subsequently
executed commands. If VALUE is supplied, assign VALUE before exporting.
Options:
-f refer to shell functions
-n remove the export property from each NAME
-p display a list of all exported variables and functions
An argument of `--' disables further option processing.
Exit Status:
Returns success …Run Code Online (Sandbox Code Playgroud) where和whichshell 命令有什么区别?这里有些例子
~ where cc
/usr/bin/cc
/usr/bin/cc
~ which cc
/usr/bin/cc
Run Code Online (Sandbox Code Playgroud)
和
~ which which
which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
/usr/bin/which
~ which where
/usr/bin/which: no where in (/usr/local/bin:/bin:/usr/bin:/home/bnikhil/bin:/bin)
Run Code Online (Sandbox Code Playgroud)
还
~ where which
which: aliased to alias | /usr/bin/which --tty-only --read-alias --show-dot
--show-tilde
which: shell built-in command
/usr/bin/which
/usr/bin/which
~ where where
where: shell built-in command
Run Code Online (Sandbox Code Playgroud)
对我来说,他们似乎做同样的事情,一个是内置的 shell,不太确定这与命令有什么不同?
shell-builtin ×10
bash ×6
zsh ×2
cd-command ×1
command-line ×1
control-flow ×1
declare ×1
directory ×1
function ×1
linux ×1
posix ×1
pwd ×1
shell ×1
shell-script ×1
signals ×1
suspend ×1
time-utility ×1
which ×1