有一个source
命令的描述:
source
是一个 bash shell 内置命令,它在当前 shell 中执行作为参数传递的文件的内容。它有一个同义词.
(句号)。
例如,为了进行实验,我想zsh
在我的例子中从不同的 shell 导出一个变量(在 中运行命令bash
):
$ zsh -c "export test=$(echo "hello world")"
$ echo $test
$
Run Code Online (Sandbox Code Playgroud)
它不起作用,因为该命令在zsh
子 shell 中运行,并且不是直接在bash
.
如果我以这种方式创建并获取脚本:
#!/home/linuxbrew/.linuxbrew/bin/zsh
export test=$(echo "hello world")
Run Code Online (Sandbox Code Playgroud)
$ chmod 777 test.zsh
$ source test.zsh
$ echo $test
hello world
Run Code Online (Sandbox Code Playgroud)
工作正常。
问题是如何在source
不使用脚本的情况下执行命令,因为source
只能使用文件运行?我想实现这样的目标:
source zsh -c "export test=$(echo "hello world")"
Run Code Online (Sandbox Code Playgroud)
如果不可能,请解释原因。
我有时会在 bash 脚本中看到类似的注释框架:
\n#!/bin/bash\n#===================================================================================\n#\n# FILE: stale-links.sh\n#\n# USAGE: stale-links.sh [-d] [-l] [-oD logfile] [-h] [starting directories]\n#\n# DESCRIPTION: List and/or delete all stale links in directory trees.\n# The default starting directory is the current directory.\n# Don\xe2\x80\x99t descend directories on other filesystems.\n#===================================================================================\n
Run Code Online (Sandbox Code Playgroud)\n是否有任何程序可以生成这样的装饰以供评论,或者人们通常手动创建它?
\nPS 经过一番搜索,我发现了类似的线程:
\n How can I create a message box from the command line?
\n bash 脚本,在框中回显输出
我有以下示例:
$ a="$(ls)"
Run Code Online (Sandbox Code Playgroud)
$ echo $a
backups cache crash lib local lock log mail opt run snap spool tmp
$
$ echo "$a"
backups
cache
crash
lib
local
lock
log
mail
opt
run
snap
spool
tmp
Run Code Online (Sandbox Code Playgroud)
现在printf
:
$ printf $a
backups
$
$ printf "$a"
backups
cache
crash
lib
local
lock
log
mail
opt
run
snap
spool
tmp
Run Code Online (Sandbox Code Playgroud)
为什么输出如此不同?在这种情况下,报价有什么作用?有人可以解释一下这是怎么回事吗?
PS 找到了有关该ls
行为的一些解释:
ls 的输出有换行符,但显示在单行上。为什么?
https://superuser.com/questions/424246/what-is-the-magic-separator- Between-filenames-in-ls-output
http://mywiki.wooledge.org/ParsingLs
可以通过这种方式检查换行符:
ls | od -c
Run Code Online (Sandbox Code Playgroud)