This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.
Related
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Bash - variable variables [duplicate]
(4 answers)
Closed 2 years ago.
I recently started bash scripting and got stuck with a very basic usecase, searched stackoverflow/google but couldn't find a way to achieve what I am trying to do.
I have a script color.sh
#!/bin/bash
Apple="Red"
Orange="Orange"
Banana="Yello"
echo $$1
What I am trying to achieve is print the color of fruit and accept fruit from command line. The output I want is
./color.sh Apple -> Red, but what I get is some random number which I think is process Id.
This question already has answers here:
How do I parse command line arguments in Bash?
(40 answers)
How to get exact command line string from shell?
(2 answers)
Closed 3 years ago.
Suppose my script.sh could take a number of options and arguments. What is the best way to find out what the script was invoked with (form inside the script)?
For eg., someone called it with script.sh --foo_option bar_arg
Is there a way to echo that exact command they typed from inside the script?
I've tried echo !! which does not work inside a script.
This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Closed 4 years ago.
Is there a way to copy the content of a text file to a variable in Bash?
Let's say I have a file containing some text, and I would like to modify text but not a file itself. How can I copy the content of this file to a variable, and then modify the variable?
I'm not very clear on what you're asking, but I think this is what you're after.
if you have file.txt, you can do this
var1=$(cat /path/to/file.txt)
you can then manipulate it how you please.
edit:
You can access the variable by $var1, i.e. echo "$var1"
This question already has answers here:
How can I remove the extension of a filename in a shell script?
(15 answers)
Closed 5 years ago.
I have hundreds files need to loop through for an analysis using a bash script. One step I need to do is to split a long string and cat it as an output name. For example, suppose I have one string such like:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final.bam
What I wanted is to rename it as two output file names such as:
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R1.fastq
5018.a.Radiation_Induced_Lymphoma.Tumor__p53+_-.SL200300_SL200300.exome_1tier.mm10.kapa_re_cap_v6_3utr.final_R2.fastq
The only changes are removing .bam from the original and cat _R1.fastq and _R2_fastq. Does somebody know how to realize it using bash commands?
somefile=blahblahblah.final.bam
foo "$somefile" "${somefile%.*}_R1.fastq" "${somefile%.*}_R2.fastq"
This question already has an answer here:
Using a variable containing spaces as a single argument
(1 answer)
Closed 8 years ago.
I am writing a bash script and i have the following:
#!/bin/bash
echo Enter some text
read tweet
t update $tweet
I have sferik t installed to tweet using the t update command and i want to pass multiple words into the $tweet variable however i am reciving this message.
ERROR: "t update" was called with arguments ["sdfs", "sdfsdf"]
Usage: "t update [MESSAGE]"
You need to quote the variable so it's passed as a single argument to t:
t update "$tweet"
In general, always quote your variables unless you know exactly why you should not.