How to add quotes around each word stored in a variable in a shell script


seniority

I have a variable foo.

echo "print foo" "$foo" ---> abc,bc,cde

I want to put quotes around each variable.

Expected result = 'abc','bc','cde'.

I have tried this way but it doesn't work:

join_lines() {
  local IFS=${1:-,}
  set --
  while IFS= read -r line; do set -- "$@" "$'line'"; done
  echo "$*"
}
RavinderSingh13

Can you try to follow, write strictly and test with the examples shown in GNU awk.

No loops:

var="abc,bc,cde"
echo "$var" | awk -v s1="'" 'BEGIN{FS=",";OFS="\047,\047"} {$1=$1;$0=s1 $0 s1} 1'

Common way of looping through all fields (comma separated):

var="abc,bc,cde"
echo "$var" | awk -v s1="'" 'BEGIN{FS=OFS=","} {for(i=1;i<=NF;i++){$i=s1 $i s1}} 1'

The output will be 'abc','bc','cde'.

Related


How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to add double quotes to lines in shell script?

Love I have a file with about 100 email IDs (one per line). I just want to add double quotes to each email id. I'm better looking for a shell script. Can someone help me get this. Shravan Yadav try awk awk '{print "\x22"$0"\x22"}' inputfilename here I am sed

How to add single quotes to each word in a string

AbhinavVaidya8 I have several parameters in my variable. I want to replace each variable with single quotes, separated by commas. var_list=emp location branch. I want my output like: var_list='emp', 'location', 'branch' Codeforester Using Bash parameter ex

Add double quotes at the first word of each line

Pushpa I have an R file with results like this: filename totalvar result runtime file1 100 0 20.45 file2 400 4 4.50 ... filen 200 1 2.00 Some filenames contain strange characters so I have to put quotes in them. What's the easiest way to put quotes aroun

Add quotes to each word in the file

jitter I have some comma separated words in a file like this: variable1, variable2, variable3, variable4 What's the easiest way to add quotes to each word using BASH? The final result should look like this: "variable1", "variable2", "variable3", "variable4"

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to put quotes around variables in shell script

Kush Patel My code is as follows file="test.text" while IFS= read line do # display $line or do somthing with $line x="\'$line\'" echo $x # sleep 10 done <"$file" However, this gives me the following result. \'google.com Any help is appr

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

put single quotes around each word

User 123 I have a newline separated list of words like: apple ball cat dog I want to separate each word with a comma and then put single quotes around each word. I can remove the newline and put a comma, but I can't put single quotes around every word. I woul

How to add double quotes to lines in shell script?

Love I have a file with about 100 email IDs (one per line). I just want to add double quotes to each email id. I'm better looking for a shell script. Can someone help me get this. Shravan Yadav try awk awk '{print "\x22"$0"\x22"}' inputfilename here I am sed

How to add double quotes to the first word of each line via sed?

tester I have a file with similar content, but I want it to be valid JSON I need to add each word of a line in double quotes. I tried the existing answers on StackOverflow and none of them worked. {container:"proxy", endpoint:"proxy", exception:"ApiException

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

How to add quotes around each word in a string in R?

Thomas Navarro I have a string: words<-"Monday, Tuesday, Wednesday, Thursday,Friday" And I just need to put quotes around each word: "Monday", "Tuesday", "Wednesday", "Thursday","Friday" Get the length of five strings. I know there are many articles on this

How to put quotes around variables in shell script

Kush Patel My code is as follows file="test.text" while IFS= read line do # display $line or do somthing with $line x="\'$line\'" echo $x # sleep 10 done <"$file" However, this gives me the following result. \'google.com Any help is appr

How to add single quotes to each word in a string

AbhinavVaidya8 I have several parameters in my variable. I want to replace each variable with single quotes, separated by commas. var_list=emp location branch. I want my output like: var_list='emp', 'location', 'branch' Codeforester Using Bash parameter ex

How to put quotes around variables in shell script

Kush Patel My code is as follows file="test.text" while IFS= read line do # display $line or do somthing with $line x="\'$line\'" echo $x # sleep 10 done <"$file" However, this gives me the following result. \'google.com Any help is appr

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

How to pass variable value using double quotes in shell script

bugCracker USER_UID=$1 echo 'generate_token("$USER_UID")' I want output like generate_token("1234567") I've tried multiple ways without success. It just prints the same line without the value generate_jwt("$USER_UID") Shane Bishop When single quotes are used

put single quotes around each word

User 123 I have a newline separated list of words like: apple ball cat dog I want to separate each word with a comma and then put single quotes around each word. I can remove the newline and put a comma, but I can't put single quotes around every word. I woul

How to add double quotes to lines in shell script?

Love I have a file with about 100 email IDs (one per line). I just want to add double quotes to each email id. I'm better looking for a shell script. Can someone help me get this. Shravan Yadav try awk awk '{print "\x22"$0"\x22"}' inputfilename here I am sed