Store output of a command in a variable [closed] - bash

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
In my mac terminal I am trying to store the output of a command into a variable like this
$ animals=$(cat animals.txt | head -n $plusOne | tail -n $numberofanimals | sort -u | sed -E "s/[*#/#2]//g; s/D/d/g ; s/G/g/g ; s/E/e/g ; s/H/h/g ; s/O/o/g ; s/C/c/g ; s/[!]//gā€)
>
but it does not work. When I press Enter I get a > prompt on the next line. Any idea why?

You've got a smart quote ā€ at the end of the line instead of a regular double quote ".

Related

How to assign extracted substring to variable in bash? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
In bash shell how do we assign extracted substring into variable without 'bad substitution' errors, please?
For example with the codes below:
fruit="apple"
echo $fruit | cut -c1-3
variable=$"{echo $fruit | cut -c1-3}"
The second line returns 'app' but the third line can't really work without bad substitution error.
Replace
variable=$"{echo $fruit | cut -c1-3}"
with
variable=$(echo $fruit | cut -c1-3)
do this instead
$ fruit=apple; var="${fruit:0:3}"; echo "$var"
app

unable to use sed command in macos? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I want to write a line mac .bash_profile file while executing a shell script.I have tried below way but it gives error as
sed -i 'export PATH='$PWD:'$PATH' $HOME/.bash_profile
error
sed: 1: "/Users/dhiraj/.bash_pro ...": extra characters at the end of d command
Kindly let me know how do i resolve it ?
If you just want to append a line to your .bash_profile, what about
echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile
?
And, if you want to be sure to insert only once:
grep -q -x -F 'PATH=\"$PWD:$PATH\"' $HOME/.bash_profile || echo "PATH=\"$PWD:$PATH\"" >> $HOME/.bash_profile

How to use read <<< in bash shell? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 5 years ago.
Improve this question
I'm trying to run this code in bash but I can't. Bash doesn't run it! it's like it's waiting for me to continue and it doesn't think that my command is done! But I'm already done! I know that the problem is with read <<<. so how can I fix it?
Here is my code:
for f in `ls *| head -100`; do a=$(file "$f" | grep -Po ", \K\d+x\d+"`);
> IFS="x";
> read x y <<< "$a";
> done;
>
>
You have a superfluous backtick:
for f in `ls *| head -100`; do a=$(file "$f" | grep -Po ", \K\d+x\d+"`);
# Here ---^
IFS="x";
read x y <<< "$a";
done;
ShellCheck helpfully points this out. Voting to close as a simple typographical error.

How do I run two commands in a single line for loop in bash? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
The community reviewed whether to reopen this question 3 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
Why can't I run two commands within a single line bash loop?
$ for i in {1..100} do printf %s "$(date)" ; mysql -uroot -e "SHOW SLAVE STATUS\G" | grep "Seconds_Behind_Master" ; sleep 10 ; done
-bash: syntax error near unexpected token `mysql'
But this simple version works:
for i in {1..3}; do echo $i ; ls ; done
You need a ; after your brace expansion. You have it in the simple example, but not in the "broken" one:
for i in {1..100}; do printf %s "$(date)" ; mysql -uroot -e "SHOW SLAVE STATUS\G" | grep "Seconds_Behind_Master" ; sleep 10 ; done
^ this one

if statement not excluding empty string variables [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am just wondering (and I'm stumped), why the if statement isn't working as it should. Have I missed something simple?
#!/bin/bash
echo -e "\nFinding the thing things on the thing, presently.\n"
DEVICE=$1
IPADDRESS=$(/sbin/ifconfig $DEVICE | grep -A 0 'inet addr:' | cut -c21-29)
for i in {1..255}
do
HOST=$(ping -c 1 $IPADDRESS.$i | grep -A 0 'from' | cut -c15-25)
if ["$HOST" != ""]
then
echo "$HOST is ALIVE!!!"
fi
done
You need to add spaces between:
if [ "$HOST" != "" ]

Resources