试图从 bash 错误中的字符串中检索前 5 个字符?

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

Trying to retrieve first 5 characters from string in bash error?

bashshell

提问by Mo.

I'm trying to retrieve the first 5 characters from a string and but keep getting a Bad substitutionerror for the string manipulation line, I have the following lines in my teststring.shscript:

我正在尝试从字符串中检索前 5 个字符,但不断收到Bad substitution字符串操作行的错误,我的teststring.sh脚本中有以下几行:

TESTSTRINGONE="MOTEST"

NEWTESTSTRING=${TESTSTRINGONE:0:5}
echo ${NEWTESTSTRING}

I have went over the syntax many times and cant see what im doing wrong

我已经多次检查语法,但看不出我做错了什么

Thanks

谢谢

回答by Alex L

Depending on your shell, you may be able to use the following syntax:

根据您的 shell,您可以使用以下语法:

expr substr $string $position $length

So for your example:

所以对于你的例子:

TESTSTRINGONE="MOTEST"
echo `expr substr ${TESTSTRINGONE} 0 5`


Alternatively,

或者,

echo 'MOTEST' | cut -c1-5

or

或者

echo 'MOTEST' | awk '{print substr(
$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=${TESTSTRINGONE:0:5}
$ echo ${NEWTESTSTRING}
MOTES
,0,5)}'

回答by Balaswamy Vaddeman

echo 'mystring' |cut -c1-5is an alternative solution to ur problem.

echo 'mystring' |cut -c1-5是您问题的替代解决方案。

more on unix cut program

更多关于unix cut 程序

回答by Balaswamy Vaddeman

Works here:

在这里工作:

 printf "%.5s" $TESTSTRINGONE

What shell are you using?

你用的是什么壳?

回答by Gordon Davisson

Substrings with ${variablename:0:5}are a bash feature, not available in basic shells. Are you sure you're running this under bash? Check the shebang line (at the beginning of the script), and make sure it's #!/bin/bash, not #!/bin/sh. And make sure you don't run it with the shcommand (i.e. sh scriptname), since that overrides the shebang.

子字符串${variablename:0:5}是 bash 功能,在基本 shell 中不可用。你确定你是在 bash 下运行的吗?检查 shebang 行(在脚本的开头),并确保它是#!/bin/bash,不是#!/bin/sh。并确保您没有使用sh命令 (ie sh scriptname)运行它,因为它会覆盖 shebang。

回答by potong

This might work for you:

这可能对你有用:

TESTSTRINGONE="MOTEST"
NEWTESTSTRING=${TESTSTRINGONE%"${TESTSTRINGONE#?????}"}
echo ${NEWTESTSTRING}
# MOTES

回答by Lauri

Works in most shells

适用于大多数 shell

[jaypal:~/Temp] TESTSTRINGONE="MOTEST"
[jaypal:~/Temp] sed 's/\(.\{5\}\).*//' <<< "$TESTSTRINGONE"
MOTES

回答by jaypal singh

You can try sedif you like -

喜欢的可以试试sed

read -n 5 NEWTESTSTRING <<< "$TESTSTRINGONE"

回答by glenn Hymanman

That parameter expansion should work (what version of bash do you have?)

该参数扩展应该可以工作(你有什么版本的 bash?)

Here's another approach:

这是另一种方法:

echo $TESTSTRINGONE|awk '{print substr(
$ TESTSTRINGONE="MOTEST"
$ NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})
$ echo $NEWTESTSTRING
MOTES
,0,5)}'

回答by Vijay

##代码##

回答by amc

You were so close! Here is the easiest solution: NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})

你离得太近了!这是最简单的解决方案:NEWTESTSTRING=$(echo ${TESTSTRINGONE::5})

So for your example:

所以对于你的例子:

##代码##