非常简单的 OSX bash 脚本:echo 没有输出

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4967087/
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-17 23:26:34  来源:igfitidea点击:

very simple OSX bash script: echo gives no output

macosbash

提问by maackle

I'm running OSX 10.6, and I have this very simple script in a file "hi.sh":

我正在运行 OSX 10.6,并且在“hi.sh”文件中有这个非常简单的脚本:

#!/bin/bash
echo "hi"

I've already run "chmod +x hi.sh". Now, I've tried both of the following:

我已经运行了“chmod +x hi.sh”。现在,我已经尝试了以下两种方法:

$ ./hi.sh
$ bash hi.sh

and neither outputs "hi". What could be the problem?

并且都不输出“hi”。可能是什么问题呢?

回答by Michael J. Barber

It could be that you used an editor that defaults to \ras line separator, which was the separator used under classic (pre OS X) MacOS. Try wc -l hi.sh; if you get 0 lines, then it's a line ending problem. Your script would then be just a single comment line, which would of course do nothing.

可能是您使用了默认\r为行分隔符的编辑器,这是在经典(OS X 之前)MacOS 下使用的分隔符。尝试wc -l hi.sh; 如果你得到 0 行,那么这是一个行尾问题。您的脚本将只是一个单独的注释行,当然什么也不做。