使用 echo 生成 bash 脚本,shebang 行出现问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6907531/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 00:29:43  来源:igfitidea点击:

Generating a bash script with echo, problem with shebang line

bashechoshebang

提问by Isaiah

I want to explain to some friends how to add multikey support to their linux systems at bootup but first I need them to make a bash script. I want to make a simple command for them to copy and paste and I'm testing out this command I made but it keeps throwing an error. Only when I add the shebang line which, well is important.

我想向一些朋友解释如何在启动时为他们的 linux 系统添加多键支持,但首先我需要他们制作一个 bash 脚本。我想为他们制作一个简单的命令来复制和粘贴,我正在测试我制作的这个命令,但它一直在抛出错误。只有当我添加 shebang 行时,这很重要。

$ sudo echo -e "#!/bin/bash \nxmodmap \"keysym Alt_R = Multi_key\"" > /etc.init.d/multikey.sh

Any easy way to echo a shebang line?

有什么简单的方法可以呼应shebang线?

回答by Ignacio Vazquez-Abrams

Use the other quotes.

使用其他引号。

sudo echo -e '#!/bin/bash\nxmodmap "keysym Alt_R = Multi_key"'

回答by hmontoliu

If you want to impress your friends use here documents not echo strings :-)

如果您想给您的朋友留下深刻印象,请使用此处的文档而不是回显字符串:-)

~$ cat << EOF > /etc/init.d/multikey.sh
> #!/bin/bash          
> xmodmap "keysym Alt_R = Multi_key"
> EOF