我有一个变量var在Bash脚本中持有一个字符串:
echo $var
"some string.rtf"
我想删除这个字符串的最后四个字符,并将结果分配给一个新变量var2,以便
echo $var2
"some string"
我该怎么做呢?
我有一个变量var在Bash脚本中持有一个字符串:
echo $var
"some string.rtf"
我想删除这个字符串的最后四个字符,并将结果分配给一个新变量var2,以便
echo $var2
"some string"
我该怎么做呢?
当前回答
这也可以做到:
... | head -c -1
-c, --bytes=[-]NUM
print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file
其他回答
希望下面的例子能有所帮助,
echo ${名称:0:$((${#名称}-10 ))} --> ${ 名称:开始:len}
在上面的命令中,name是变量。 Start是字符串的起始点 Len是必须删除的字符串长度。
例子:
read -p "Enter:" name
echo ${name:0:$((${#name}-10))}
输出:
Enter:Siddharth Murugan
Siddhar
注意:Bash 4.2增加了对负子字符串的支持
要从字符串末尾删除四个字符,请使用${var%????}。
去掉所有的东西,包括最后的。使用$ {var %。*}。
更多信息请参见Bash的参数展开文档。
这也可以做到:
... | head -c -1
-c, --bytes=[-]NUM
print the first NUM bytes of each file; with the leading '-', print all but the last NUM bytes of each file
首先,明确你的意图通常会更好。因此,如果你知道字符串以你想要删除的.rtf结尾,你可以使用var2=${var%.rtf}。这种方法的一个潜在有用的方面是,如果字符串不以.rtf结尾,则它根本不会改变;Var2将包含一个未修改的var副本。
If you want to remove a filename suffix but don't know or care exactly what it is, you can use var2=${var%.*} to remove everything starting with the last .. Or, if you only want to keep everything up to but not including the first ., you can use var2=${var%%.*}. Those options have the same result if there's only one . in the string, but if there might be more than one, you get to pick which end of the string to work from. On the other hand, if there's no . in the string at all, var2 will again be an unchanged copy of var.
如果你真的想总是删除特定数量的字符,这里有一些选项。
You tagged this bash specifically, so we'll start with bash builtins. The one which has worked the longest is the same suffix-removal syntax I used above: to remove four characters, use var2=${var%????}. Or to remove four characters only if the first one is a dot, use var2=${var%.???}, which is like var2=${var%.*} but only removes the suffix if the part after the dot is exactly three characters. As you can see, to count characters this way, you need one question mark per unknown character removed, so this approach gets unwieldy for larger substring lengths.
An option in newer shell versions is substring extraction: var2=${var:0:${#var}-4}. Here you can put any number in place of the 4 to remove a different number of characters. The ${#var} is replaced by the length of the string, so this is actually asking to extract and keep (length - 4) characters starting with the first one (at index 0). With this approach, you lose the option to make the change only if the string matches a pattern. As long as the string has at least four characters, no matter what its actual value is, the copy will include all but its last four characters.
You can leave the start index out; it defaults to 0, so you can shorten that to just var2=${var::${#var}-4}. In fact, newer versions of bash (specifically 4+, which means the one that ships with MacOS won't work) recognize negative lengths as the index of the character to stop at, counting back from the end of the string. So in those versions you can get rid of the string-length expression, too: var2=${var::-4}. This interpretation is also triggered if you leave the string length in but the string is shorter than four characters, since then ${#var}-4 is negative. For example, if the string has three characters, ${var:0:${#var}-4} becomes ${var:0:-1} and removes only the last character.
If you're not actually using bash but some other POSIX-type shell, the pattern-based suffix removal with % will still work – even in plain old dash, where the index-based substring extraction won't. Ksh and zsh do both support substring extraction, but require the explicit 0 start index; zsh also supports the negative end index, while ksh requires the length expression. Note that zsh, which indexes arrays starting at 1, nonetheless indexes strings starting at 0 if you use this bash-compatible syntax. But zsh also allows you to treat scalar parameters as if they were arrays of characters, in which case the substring syntax uses a 1-based count and places the start and (inclusive) end positions in brackets separated by commas: var2=$var[1,-5].
当然,您可以不使用内置的shell参数展开,而是运行一些实用程序来修改字符串并使用命令替换捕获其输出。有几个命令可以工作;一个是var2=$(sed 's/)。\ {4 \}$//' <<<"$ var”)。
使用变量展开/子字符串替换:
$ {var / %模式/替代} 如果var的后缀匹配Pattern,则用Replacement替换Pattern。
所以你可以这样做:
~$ echo ${var/%????/}
some string
另外,
如果你总是有相同的4个字母
~$ echo ${var/.rtf/}
some string
如果它总是以。xyz结尾:
~$ echo ${var%.*}
some string
你也可以使用字符串的长度:
~$ len=${#var}
~$ echo ${var::len-4}
some string
或者直接返回${var::-4}