Put pipe value into variable [duplicate] - bash

This question already has an answer here:
How to redirect grep output to a variable?
(1 answer)
Closed 8 years ago.
I'm new in bash scripting world...
I'm trying to get value from pipe action to a var.
Something like:
result = $(ls /usr/bin | dmenu)
the idea is put files list into a standar menu (dmenu) so, when user select
a choice, i want to know wich one is selected and work with this option
to for example, execute a file.
the $result is not getting any value.
Thanks for your help

Remove whitespaces before and after =:
result=$(ls /usr/bin | dmenu)

Related

echo file names in reverse order [duplicate]

This question already has answers here:
How to reverse a list of words in a shell string?
(18 answers)
Sort 'ls' output by name
(13 answers)
Closed 7 months ago.
This post was edited and submitted for review 7 months ago and failed to reopen the post:
Duplicate This question has been answered, is not unique, and doesn’t differentiate itself from another question.
I would like to list files in reverse order, list of files listed are as below, the purpose is to echo all the file contents into another file.
vcp.status-200.txt
vcp-status-400.txt
vcp-status-500.txt
vcp-status-000.txt
I am currently running below command; echo will list all the files and xargs cat will append the content of these files into OutputFile.
echo *${USER}*status*.txt | xargs cat >> OutputFile
Current Output
vcp-status-000.txt vcp-status-200.txt vcp-status-400.txt vcp-status-500.txt
Intended Output
vcp-status-500.txt vcp-status-400.txt vcp-status-200.txt vcp-status-000.txt

Combine two variables to form the identifier for another variable in Bash [duplicate]

This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.

Please explain what is happening with this 2 lines of bash code [duplicate]

This question already has answers here:
Assigning default values to shell variables with a single command in bash
(11 answers)
Closed 3 years ago.
im trying to learn bash right now, came across a script and im not 100% sure if i read it right.
source_dir_123=${SOURCE_DIR:-/tmp}
echo source_dir_123=$source_dir_123
What is happening here? I guess this is some kinda variable assigment, but it looks weird to me.
What type of assigment/operation happens here? Any specific name of these types of assignments?
Sorry for a newbish question, but i dont get it why would you use these kinda of assignments instead of something more straight forward like
source_dir_12="/tmp"
/tmp is the default value for source_dir_123 in case SOURCE_DIR is not set, then you're displaying the result to the console.
See the following example:
> echo $SOURCE_DIR
> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/tmp
# now let's set SOURCE_DIR
> SOURCE_DIR=/test
> source_dir_123=${SOURCE_DIR:-/tmp}
> echo source_dir_123=$source_dir_123
source_dir_123=/test

Appending text to the end of a variable [duplicate]

This question already has answers here:
How to concatenate string variables in Bash
(30 answers)
Closed 8 years ago.
The following works, but I don't want the space that it returns:
read input
file= "$input"
file= "$file ins.b" # how to get rid of the space here?
echo "$file"
This outputs 'file ins.b'
I don't want the space between file and ins.b
If I don't leave that space in the code it returns only '.b'. What can I do to resolve this problem?
Append like:
file="${file}ins.b"
If you don't use braces then it treats fileins as a variable and expands it. Since, it's probably not set it just prints .b.
Related: When do we need curly braces in variables using Bash?
In bash you can also reference variables like ${file}. So this should work for you:
file="${file}ins.b"
You don't need to expand the old value at all; bash has a += operator:
file+="ins.b"
file="${file}ins.b"
or
file=$file"ins.b"

How to define dynamic variable in bash? [duplicate]

This question already has answers here:
How to create a bash variable like $RANDOM
(3 answers)
Closed 5 years ago.
I would like to have a shell variable that could be dynamically run every time it is refered, for example, i would like to have a variable $countPwd which could return the count of files/dirs in the current directory, it could be defined as:
countPwd=`ls | wc -l`
and if I do echo $countPwd it would only show the value when I define the variable, but it won't update automatically when I change my current directory. So how do I define such a variable in bash that the value of it get updated/calculated on the fly?
Update:
The $PWD is a perfect example of a variable get evaluated in the real time. You don't need to use $() or backticks `` to evaluate it. How is it defined in bash?
Make a function:
countPwd() {
ls | wc -l
}
Then call the function like any other command:
echo "There are $(countPwd) files in the current directory."
Another option: store the command in a variable, and evaluate it when required:
countPwd='ls | wc -l'
echo $(eval "$countPwd")
You'll need to write some C code: write a bash's loadable buitins to do the heavy lifting in defining variables with dynamic values like $SECONDS or $RANDOM ($PWD is just set by cd).
More details in my answer here (a duplicate of this question).

Resources