"回声-n"使用bash执行脚本时,工作正常,但不是SH [英] "echo -n" works fine when executing script with bash, but not with sh

查看:118
本文介绍了"回声-n"使用bash执行脚本时,工作正常,但不是SH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么 SH UsersInput.sh 给出了不同的输出相比,庆典UsersInput.sh

How come sh UsersInput.sh gives a different output compared to bash UsersInput.sh?

我的脚本如下:

#!/bin/bash
echo -n "Enter: ";
read usersinput;
echo "You entered, \"$usersinput\""; 

庆典

localhost:Bash henry$ bash UsersInput.sh 
Enter: input
You entered, "input"

SH

localhost:Bash henry$ sh UsersInput.sh
-n Enter: 
input
You entered, "input"

为什么 -n 正确的第一表现,而与第二?什么是这样做的原因,是有解决方法吗?

How come -n behaves properly with the first, but not with the second? What's the reason for this and is there a workaround?

推荐答案

人回声

有些shell可以提供一个内置的echo命令是相似或相同此实用程序。最值得注意的是,在SH的内置回波(1)不接受-n选项。咨询内建(1)手册页。

Some shells may provide a builtin echo command which is similar or identical to this utility. Most notably, the builtin echo in sh(1) does not accept the -n option. Consult the builtin(1) manual page.

在bash中,是Bourne-Again Shell的,回声接受 -n 选项,而在SH,伯恩外壳,回声没有,所以它只是回声你写的一切,包括-n。

In bash, the Bourne-again shell, echo accepts the -n option whereas in sh, the Bourne shell, echo does not, so it simply echos everything you wrote, including the -n.

这篇关于"回声-n"使用bash执行脚本时,工作正常,但不是SH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆