Syntax error using expr to get string length [duplicate] - bash

This question already has answers here:
How to store the length of a string in a variable in bash?
(2 answers)
Closed 4 years ago.
On mac running 4.4.23(1), an example I've seen used for finding the length of a string throws an error:
string_var=blah
echo `expr length $string_var`
expr: syntax error
Works fine on my Debian system.
shopt options are: himvBHs

expr is not part of bash -- it's an ancient UNIX tool from back when the shell couldn't do math (or much else useful) on its own.
You don't need it. In the modern day, ${#var} will give you the length of the value assigned to var, as follows:
string_var=blah
echo "${#string_var}"

Related

Why do I get the error of "expr: syntax error" in a simple shell script [duplicate]

This question already has answers here:
macOS Mojave version 10.14.1 bash-3.2 expr: syntax error
(1 answer)
Bash: Find position of character in a string under OS X
(2 answers)
Closed 2 years ago.
My script is
#! /bin/bash
v1="hello"
v2="world1"
subIndex=`expr index "${v1}" "${v2}"`
echo $subIndex
and I got the error of "expr: syntax error"
It looks like you're using a version of expr which doesn't have an index operator.
As you can see in the POSIX standard man page there's no index in the table of operators. The only mention of index in that document says that the behaviour is undefined. We can assume that the standardisation process saw that only some implementations supported index and some or all of the participants did not want to add the additional functionality.
Some other implementations contain additional functionality above and beyond what is specified in the standard. You'll need to install one of these implementations or change your code. GNU expr is one implementation that does have index, length, etc..

Bash - Add variable in associative array - bad substitution [duplicate]

This question already has answers here:
Bash indirect variable referencing
(1 answer)
Bash indirect array addressing?
(3 answers)
Closed 2 years ago.
Here is an expected behavior for associated array in bash
$ declare -A PC=( [Monitor]=Dell [CPU]=HP )
$ echo ${PC[CPU]}
HP
This gives me HP as output
Lets say I have these PC,Monitor amd CPU values stored in variable a , b and c. I am trying fetch the details now but I am getting "bad substitution" error when trying so.
$ a=PC; b=Dell; c=HP
$ echo ${$a[$b]}
bash: ${$a[$b]}: bad substitution
$ echo ${PC[$b]}
Dell
${PC[$b]} however is returning expected output but not {$a[$b]}
Not sure how this can be achieved. Thanks in advance.
What you are trying to do is called indirection - using one variable as the name of another variable.
In bash you do this for normal variables using the syntax ${!var}, as in
a=5
b=a
echo ${!b} # 5
Unfortunately this won't work how you want for an array variable because the syntax ${!array[*]} means something else (getting all keys from an associative array).
Instead, as suggested by a comment below, you can create a string for the entire reference and then use redirection on that:
lookup="$a[$b]"
echo ${!lookup} # will give Dell in your example

BASH remove specific tokens from a word [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 5 years ago.
I am trying to find te longest word in a given file. Before I check the lengtgh of each word I need to remove all of the following tokens {,.:} that may be attached (once or more) to the word. so for example, for this text:
:,cat dog, encyclopedia; remove:.,
i need the result:
cat dog encyclopedia remove
I am trying this, but I get a "command not found":
longest=0
for word in $(<$1)
do
#new_word = $(echo "${word//[.,:]/}")
new_word = "${word//[.,:]/}"
len=${#new_word}
if (( len > longest ))
then
longest=$len
longword=$new_word
fi
done
echo The longest word is $longword and its length is $longest.
thank you.
Your use of parameter expansion replacement pattern is correct.
The problem is that there must not be any whitespace around = while declaring variables in bash (any shell in general).
So, the following should work:
new_word="${word//[.,:]/}"
As an aside, use a while read ... construct to loop over the lines in a file, using for is pretty fragile.

difference between $[a-b] and $((a-b)) in bash [duplicate]

This question already has answers here:
bash: $[<arithmetic-expression>] vs. $((<arithmetic-expression>))
(2 answers)
Closed 6 years ago.
I don’t know this operator $[] and couldn’t find something about it. However I know that next two codes give the same output
a=4
b=1
echo $[a-b] # => 3
and
a=4
b=1
echo $((a-b)) # => 3
So what is $[] operator for, and what’s the difference with $(()) ?
In my zsh shell prompt, when I open any of them and no close them, I have mathsubst written.
Reading man bash says that the old format $[expression] is deprecated and will be removed. Otherwise they should be equivalent.
Arithmetic expansion allows the evaluation of an arithmetic expression
and the substitution of the result. The format for arithmetic
expansion is:
$((expression))
The old format $[expression] is deprecated and will be removed in
upcoming versions of bash.

Super Simple bash If (variable > integer) treating (variable as a command [duplicate]

This question already has answers here:
Getting "command not found" error while comparing two strings in Bash
(4 answers)
Closed 6 years ago.
I feel like this should be really simple but I can't get past this step.
$num = 5
if [$num > 2]; then echo greater; fi
the problem is, I keep getting [5: command not found.
Why is it not evaluating the if [ test ] block correctly? It's like the shell forgot about the if and just moved on to "hey, [5 > 2 does not look like a command, I can't find [5"... but [5 isn't a command, it's part of the if test block?
I have tried using different brackets and using -gt instead of >. The problem is bash doesn't actually ever do the test. For some reason it ignores if.
Just managed to find the answer:
if compare strings get a "command not found"-Error
really unexpected, there needs to be a space between the [ brackets and the variable.
I would never have guessed.

Resources