Copying text file to a variable in Bash [duplicate] - bash

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"

Related

How to execute a command using a line from a text file? [duplicate]

This question already has answers here:
Unix: How to delete files listed in a file
(13 answers)
Shell command/script to delete files whose names are in a text file
(7 answers)
Closed 2 years ago.
I have a file with several paths to other files.
/root/Desktop/test1/test2/f1
/root/Desktop/test1/test2/f2
I need to execute a command from the terminal to delete f1 and f2 by importing their paths from that file.
Something like "rm code"
code = line 1 of the file text
Is there a way to do so ?
rm root/Desktop/test1/test2/f*
should do if I understood your question.

bash string split and concatenate [duplicate]

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"

What is {} used for in this context? [duplicate]

This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
It is doing something to my path_bash variable but what?
Google pulls up this but I can't find it exactly.
#!/bin/bash
path_bash="$HOME/root/config/bash/"
source "${path_bash}_private.sh"
source "${path_bash}config.sh"
source "${path_bash}utility.sh"
source "${path_bash}workflow.sh"
source "${path_bash}net.sh"
source "${path_bash}makeHTM.sh"
and can I put
path_bash
in another file?
It's used to tell bash where the name of your variable ends.
And example to explain:
$ a="gg"
$ echo $ab
$ echo ${a}b
ggb

Bash Script Requires Enter Before Any Input [duplicate]

This question already has answers here:
Script parameters in Bash
(5 answers)
Closed 6 years ago.
I've written a simple bash script, named ocropus:
#!/bin/bash
read filename path
...
And then i realized that I can't run it like this:
ocropus filename path
Instead, I need to run it like this:
ocropus
filename path
What can I do so I don't need to hit enter before my inputs? Thanks a lot!
Command line arguments are in $1, $2, etc. So do:
filename=$1
path=$2
instead of
read filename path

How can i get the value from a file using bash? [duplicate]

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.

Resources