Can't chop last X letters from a string [closed]checking an argument to a bash script is a string of all digitsMatching regular expression with delimited string in unixHow to append extension to files with certain filename formatHow can I make sure that I'm running an updated version of bash?Can't use global shorcuts for Python applicationsWhy is my regex not working using sed in bash script on Mac OSX?Count the number of occurrences of a substring in a stringCompare datetime stringBash: Set variable equal to a string substitutionhow to convert a string to a float and then use it in a conditional operation?
Is it possible to do 50 km distance without any previous training?
Why is the design of haulage companies so “special”?
Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?
"which" command doesn't work / path of Safari?
DOS, create pipe for stdin/stdout of command.com(or 4dos.com) in C or Batch?
Why CLRS example on residual networks does not follows its formula?
Are tax years 2016 & 2017 back taxes deductible for tax year 2018?
How much RAM could one put in a typical 80386 setup?
How can I fix this gap between bookcases I made?
N.B. ligature in Latex
What defenses are there against being summoned by the Gate spell?
Can a German sentence have two subjects?
Why don't electron-positron collisions release infinite energy?
whey we use polarized capacitor?
What typically incentivizes a professor to change jobs to a lower ranking university?
Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?
Why is an old chain unsafe?
Why Is Death Allowed In the Matrix?
How is the claim "I am in New York only if I am in America" the same as "If I am in New York, then I am in America?
Can Medicine checks be used, with decent rolls, to completely mitigate the risk of death from ongoing damage?
Is it tax fraud for an individual to declare non-taxable revenue as taxable income? (US tax laws)
Prevent a directory in /tmp from being deleted
Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?
Modification to Chariots for Heavy Cavalry Analogue for 4-armed race
Can't chop last X letters from a string [closed]
checking an argument to a bash script is a string of all digitsMatching regular expression with delimited string in unixHow to append extension to files with certain filename formatHow can I make sure that I'm running an updated version of bash?Can't use global shorcuts for Python applicationsWhy is my regex not working using sed in bash script on Mac OSX?Count the number of occurrences of a substring in a stringCompare datetime stringBash: Set variable equal to a string substitutionhow to convert a string to a float and then use it in a conditional operation?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm on a mac, bash version 5.0.2.
Got this:
v="ksdjfksjkjsdf"
v2=$v::-6
Get this:
-bash: -6: substring expression < 0
bash
closed as off-topic by Jesse_b, ilkkachu, terdon♦ Mar 27 at 19:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
add a comment |
I'm on a mac, bash version 5.0.2.
Got this:
v="ksdjfksjkjsdf"
v2=$v::-6
Get this:
-bash: -6: substring expression < 0
bash
closed as off-topic by Jesse_b, ilkkachu, terdon♦ Mar 27 at 19:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
Which version ofbash
? I just tried this with 4.4.12 and it worked as expected.
– Panki
Mar 27 at 14:30
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you triedv2=$v::-14
it would
– Jesse_b
Mar 27 at 14:42
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49
add a comment |
I'm on a mac, bash version 5.0.2.
Got this:
v="ksdjfksjkjsdf"
v2=$v::-6
Get this:
-bash: -6: substring expression < 0
bash
I'm on a mac, bash version 5.0.2.
Got this:
v="ksdjfksjkjsdf"
v2=$v::-6
Get this:
-bash: -6: substring expression < 0
bash
bash
asked Mar 27 at 14:27
StevieDStevieD
1659
1659
closed as off-topic by Jesse_b, ilkkachu, terdon♦ Mar 27 at 19:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
closed as off-topic by Jesse_b, ilkkachu, terdon♦ Mar 27 at 19:51
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "Questions describing a problem that can't be reproduced and seemingly went away on its own (or went away when a typo was fixed) are off-topic as they are unlikely to help future readers." – ilkkachu, terdon
Which version ofbash
? I just tried this with 4.4.12 and it worked as expected.
– Panki
Mar 27 at 14:30
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you triedv2=$v::-14
it would
– Jesse_b
Mar 27 at 14:42
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49
add a comment |
Which version ofbash
? I just tried this with 4.4.12 and it worked as expected.
– Panki
Mar 27 at 14:30
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you triedv2=$v::-14
it would
– Jesse_b
Mar 27 at 14:42
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49
Which version of
bash
? I just tried this with 4.4.12 and it worked as expected.– Panki
Mar 27 at 14:30
Which version of
bash
? I just tried this with 4.4.12 and it worked as expected.– Panki
Mar 27 at 14:30
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried
v2=$v::-14
it would– Jesse_b
Mar 27 at 14:42
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried
v2=$v::-14
it would– Jesse_b
Mar 27 at 14:42
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49
add a comment |
4 Answers
4
active
oldest
votes
Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?
Turns out mybash --version
returns different value than/bin/bash --version
. OP was running with#!/bin/bash
version.
– StevieD
Mar 27 at 14:59
add a comment |
Using negative lengths was added in Bash 4.2:
$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha
And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:
$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0
That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.
add a comment |
This worked:
v="aksdjfkajsdkfjakdf"
length=$#v
v2=$v:0: length - 6
This will not work if the length ofv
is less than 6 characters, which is likely your issue in the first place.
– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
|
show 4 more comments
I have done by below method
v="ksdjfksjkjsdf"
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
output
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
ksdjfks
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, orsed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t needsed
.)
– Stephen Kitt
Mar 28 at 8:23
add a comment |
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?
Turns out mybash --version
returns different value than/bin/bash --version
. OP was running with#!/bin/bash
version.
– StevieD
Mar 27 at 14:59
add a comment |
Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?
Turns out mybash --version
returns different value than/bin/bash --version
. OP was running with#!/bin/bash
version.
– StevieD
Mar 27 at 14:59
add a comment |
Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?
Are you sure you are running the script with the correct bash version? Is your shebang line ok? How about calling the script with bash myscript.sh?
answered Mar 27 at 14:38
Joni LJoni L
361
361
Turns out mybash --version
returns different value than/bin/bash --version
. OP was running with#!/bin/bash
version.
– StevieD
Mar 27 at 14:59
add a comment |
Turns out mybash --version
returns different value than/bin/bash --version
. OP was running with#!/bin/bash
version.
– StevieD
Mar 27 at 14:59
Turns out my
bash --version
returns different value than /bin/bash --version
. OP was running with #!/bin/bash
version.– StevieD
Mar 27 at 14:59
Turns out my
bash --version
returns different value than /bin/bash --version
. OP was running with #!/bin/bash
version.– StevieD
Mar 27 at 14:59
add a comment |
Using negative lengths was added in Bash 4.2:
$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha
And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:
$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0
That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.
add a comment |
Using negative lengths was added in Bash 4.2:
$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha
And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:
$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0
That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.
add a comment |
Using negative lengths was added in Bash 4.2:
$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha
And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:
$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0
That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.
Using negative lengths was added in Bash 4.2:
$PARAMETER:OFFSET:LENGTH: Negative LENGTH values are used as offset from the end of the string 4.2-alpha
And it seems to work for me with Bash versions 4.2.53, 4.4.12 and 5.0.3. With any of those, I get:
$ s=123456; echo "$s::-2"
1234
$ s=123456; echo "$s::-8"
bash: -8: substring expression < 0
That is, it works as long as the absolute value of the negative length isn't greater than the length of the string.
answered Mar 27 at 15:04
ilkkachuilkkachu
63.2k10104181
63.2k10104181
add a comment |
add a comment |
This worked:
v="aksdjfkajsdkfjakdf"
length=$#v
v2=$v:0: length - 6
This will not work if the length ofv
is less than 6 characters, which is likely your issue in the first place.
– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
|
show 4 more comments
This worked:
v="aksdjfkajsdkfjakdf"
length=$#v
v2=$v:0: length - 6
This will not work if the length ofv
is less than 6 characters, which is likely your issue in the first place.
– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
|
show 4 more comments
This worked:
v="aksdjfkajsdkfjakdf"
length=$#v
v2=$v:0: length - 6
This worked:
v="aksdjfkajsdkfjakdf"
length=$#v
v2=$v:0: length - 6
answered Mar 27 at 14:47
StevieDStevieD
1659
1659
This will not work if the length ofv
is less than 6 characters, which is likely your issue in the first place.
– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
|
show 4 more comments
This will not work if the length ofv
is less than 6 characters, which is likely your issue in the first place.
– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
This will not work if the length of
v
is less than 6 characters, which is likely your issue in the first place.– Jesse_b
Mar 27 at 14:49
This will not work if the length of
v
is less than 6 characters, which is likely your issue in the first place.– Jesse_b
Mar 27 at 14:49
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
I know it's more than 6 characters in the code it will be used in.
– StevieD
Mar 27 at 14:50
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
Why are you downvoting the correct answer? I just ran it. In this example I have a string way more than 6 characters.
– StevieD
Mar 27 at 14:51
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
You should file a bug report with bash
– Jesse_b
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
I have no idea if it's a bug or not. Could work differently in bash 5.
– StevieD
Mar 27 at 14:52
|
show 4 more comments
I have done by below method
v="ksdjfksjkjsdf"
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
output
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
ksdjfks
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, orsed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t needsed
.)
– Stephen Kitt
Mar 28 at 8:23
add a comment |
I have done by below method
v="ksdjfksjkjsdf"
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
output
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
ksdjfks
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, orsed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t needsed
.)
– Stephen Kitt
Mar 28 at 8:23
add a comment |
I have done by below method
v="ksdjfksjkjsdf"
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
output
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
ksdjfks
I have done by below method
v="ksdjfksjkjsdf"
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
output
echo $v| awk -F "" 'gsub("","n",$0);print $0'| sed '/^$/d'| sed -n '1,7p'| perl -pne g"/n//g"
ksdjfks
answered Mar 27 at 18:02
Praveen Kumar BSPraveen Kumar BS
1,7391311
1,7391311
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, orsed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t needsed
.)
– Stephen Kitt
Mar 28 at 8:23
add a comment |
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, orsed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t needsed
.)
– Stephen Kitt
Mar 28 at 8:23
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or
sed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed
.)– Stephen Kitt
Mar 28 at 8:23
Are you sure that works? I get each character on a separate line. In any case, you can do all this in a single AWK invocation, or
sed
, or Perl, there’s no need to run multiple programs. (As a general rule of thumb, if you’re using Perl, you don’t need AWK; if you’re using AWK, you don’t need sed
.)– Stephen Kitt
Mar 28 at 8:23
add a comment |
-bash
Which version of
bash
? I just tried this with 4.4.12 and it worked as expected.– Panki
Mar 27 at 14:30
at the top of the post: 5.0.2
– StevieD
Mar 27 at 14:31
Are you sure you are providing us with the exact variables you are working with? It seems the way you get this error is trying to trim more characters from a string than exist. Your example should never fail but if you tried
v2=$v::-14
it would– Jesse_b
Mar 27 at 14:42
Positive. Yes. That's all I have in my test script besides shebang.
– StevieD
Mar 27 at 14:49