bash variable string with quotes and double quotes - bash

I need to store a string in a variable on bash.
Here the string...
7;310000000007;1390;30000001390;119;130000000119;;;;;939738;30;ST;DESCRIPTION TEXT TEXT " TEXT ' TEXT text;20130318; 1.40;; 0.00;1459
Note that have both quotes and double quotes. It's possible to store it on a variable in BASH?
An important cosideration: I can't add \" or \', because is a text file with 150.000 line like the string.
Thank's!

Not really sure, but I think you can try with read, for example:
while read var
do
echo $var
done < file.txt
Reading lines like that from a file should be okay with read.

Related

Escape special characters in BASH [duplicate]

In a shell script I would like to quote this string of special characters \'%"\"'\ How do I escape the quotes/backslashes inside the string ?
If you use single quotes around the whole string, then the only thing you need to worry about is replacing every ' with '"'"':
$ string='\'"'"'%"\"'"'"'\'
$ echo "$string"
\'%"\"'\
This means:
' close the previous single-quoted string
"'" a new double-quoted string, containing a single quote
' open a new single-quoted string
The shell concatenates adjacent strings, so you get a single quote where you want it. You can replace the middle part by \' but personally I think that's more confusing!

How to quote string of special characters?

In a shell script I would like to quote this string of special characters \'%"\"'\ How do I escape the quotes/backslashes inside the string ?
If you use single quotes around the whole string, then the only thing you need to worry about is replacing every ' with '"'"':
$ string='\'"'"'%"\"'"'"'\'
$ echo "$string"
\'%"\"'\
This means:
' close the previous single-quoted string
"'" a new double-quoted string, containing a single quote
' open a new single-quoted string
The shell concatenates adjacent strings, so you get a single quote where you want it. You can replace the middle part by \' but personally I think that's more confusing!

How to make bash script take file names with spaces?

I have a bash script like this:
myfiles=("file\ with\ spaces.csv")
for file_name in "${myfiles[#]}"
do
echo "removing first line of file $file_name"
echo "first line is `head -1 $file_name`"
echo "\n"
done
but it does not recognize the spaces for some reason, even though I enclosed it in double quotes "":
head: cannot open ‘file\\’ for reading: No such file or directory
How do I fix this?
You need double quotes inside the backticks. The outer set isn't sufficient.
echo "first line is `head -1 "$file_name"`"
Also, do not put backslashes in the file name, since it's already quoted. Quotes or backslashes, but not both.
myfiles=("file with spaces.csv")
myfiles=(file\ with\ spaces.csv)
To expand on #JohnKugelman's answer:
Quoting takes a bit of getting used to in Bash. As a simple rule use single quotes for static strings with no special characters, double quotes for strings with variables, and $'' quoting for strings with special characters.
There's a separate quoting context inside every command substitution.
$() is a clearer way to establish a command substitution, because it can be nested much easier.
Consequently you'd typically write myfiles=('file with spaces.csv') and echo "first line is $(head -1 "$file_name")".

testing if input var $1 is encased in single quotes or not

I have a script that requires the user to input some text, like $ script.sh someText, but I need to ensure the input the user types is encased in single quotes like $ script.sh 'someText'. But I can't seem to figure out how to test for the quotes.
There are other limitations regarding what kind of input the user can enter, such as no spaces and of course no single quotes as part of their input, both of which I easily test for by reading $1, but how do I read if single quotes, which are correctly not read by BASH as part of the input var $1, are used to open and close the user's input?
You can't tell whether the command line was written with single quotes, double quotes or no quotes, because the shell removes them all.
If you have a script echoit:
#!/bin/bash
printf "%s\n" "$#"
Then you can run it as:
echoit No\ Quotes "Double Quotes" 'Single Quotes'
And the output will be:
No Quotes
Double Quotes
Single Quotes

Bash function, return bold text

I'm trying to write a simple bash function that returns bold text. The code I have written so far is:
function txt_bold() {<br>
echo -e '\033[1m$1\033[0m$2'<br>
tput sgr0<br>
}
When I write txt_bold "This is bold" "And this in plain text" it returns "$1$2" ($1 in bold). What am I doing wrong here?
Use " instead of '.
function txt_bold() {
echo -e "\033[1m$1\033[0m$2"
tput sgr0
}
Short
Within single quotes variables are not getting expanded.
Long
Below's the bottom line of this article, which might help you understand it: What’s the Difference Between Single and Double Quotes in the Bash Shell?
Double Quotes
Use when you want to enclose variables or use shell expansion inside a string.
All characters within are interpreted as regular characters except for $ or ` which will be expanded on the shell.
Single Quotes
All characters within single quotes are interpreted as a string character.

Resources