Tips of Bash/Zsh
本文为摘录(或转载),侵删,原文为: https://unix.stackexchange.com/questions/505949/expanding-only-certain-variables-inside-a-heredoc
1 展开 Bash 数组 (array) 时候, ${ARRARY[@]}
和 ${ARRARY[*]}
有什么区别?
INFO 中有相关说明:
数组的任何元素可以使用’${NAME[SUBSCRIPT]}‘来引用。
这些大括号是为了避免与 shell 的文件名扩展操作符产生冲突。如果 SUBSCRIPT 为’@‘或’*’,那么该词会扩展为数组 NAME 的所有成员。这些下标在词出现在双引号内时才有所不同。如果该词被双引号括起来,’\({NAME[*]}‘会扩展为单词,其值为每个数组成员之间以 IFS 变量的第一个字符分隔,而’\){NAME[@]}‘会将 NAME 的每个元素扩展为独立的单词。当数组没有成员时,’${NAME[@]}‘扩展为空。如果双引号扩展出现在一个单词中,第一个参数的扩展将与原始单词的开头部分连接,而最后一个参数的扩展将与最后部分连接。
例子:
|
|
输出为:
|
|
2 How do you escape characters in heredoc?
2.1 Question
我正在使用一个 bash 脚本,试图阻止它尝试替换 heredoc 中的变量。如何将 heredoc 设置为 A)转义变量名而不解析它们,或者 B)返回整个字符串而不改变它?
|
|
就现状而言,当我将它注入到文件中结束时,我得到的是这个:
|
|
2.2 Answer
从 bash(1) man 页:
如果 word 中的任何字符被引用,那么分隔符就是对 word 进行引号移除的结果,并且 here-document 中的行不会被展开。
|
|
|
|
3 special expansion:
|
|
4 will .bash_profile be sourced when executing scripts?
.bash_profile gets called when you login and not at restart. ~/.bash_profile is only sourced by bash when started in interactive login mode. When you log in graphically, ~/.profile will be specifically sourced by the script that launches gnome-session (or whichever desktop environment you’re using). So ~/.bash_profile is not sourced at all when you log in graphically. When you open a terminal, the terminal starts bash in (non-login) interactive mode, which means it will source ~/.bashrc. The right place for you to put these environment variables is in ~/.profile, and the effect should be apparent next time you log in. Sourcing ~/.bash_profile from ~/.bashrc is the wrong solution. It’s supposed to be the other way around; ~/.bash_profile should source ~/.bashrc. You also need to enable crond service via sys v / BSD init style system.
当您登录时会调用.bash_profile,而在重新启动时不会调用。只有当 bash 以交互式登录模式启动时,才会通过.bash_profile 进行资源引用。当您通过图形界面登录时,
/.profile 将被启动 gnome-session(或您正在使用的其他桌面环境)的脚本特别调用。因此,在图形登录时根本不会调用/.bash_profile。当您打开终端时,终端会以(非登录的)交互模式启动 bash,这意味着它将会引用~/.bashrc。您应该将这些环境变量放在~/.profile 中,下次登录时效果应该就会显现出来。从~/.bashrc 中引用~/.bash_profile 是错误的解决方案。正确的做法是相反的;/.bash_profile 应该引用/.bashrc。您还需要通过 sys v / BSD init 样式系统来启用 crond 服务。