How to remove quotes in a variable in batch script - windows

I have an issue in batch programming. I fetch the strings generated by consoles, then the string contains double quotes and I will save that to a variable, like,
"/path/compile" -o source.cpp
And now my problem is, how can I remove this 2 double quotes? I'm not sure how to remove that quotes in the middle of the string.
Please advise

set a="hello" world
set b=%a:"=%

correct syntax:
set "tempvar="/path/compile" -o source.cpp"
echo %tempvar%
set "tempvar=%tempvar:"=%"
echo %tempvar%

Related

Manipulating Single quote in string

I am running below shell script
var1="'"
json_variable=$var1{"id":158,"name":"stackoverflow"}$var1
echo $json_variable
I am getting below output
'{id:158,name:stackoverflow}'
How can I get output in below format
'{"id":158,"name":"stackoverflow"}'
Thanks,
I think you are looking for:
json_variable="'{\"id\":158,\"name\":\"stackoverflow\"}'"
or perhaps you want
json_variable="'"'{"id":158,"name":"stackoverflow"}'"'"
or
json_variable=\''{"id":158,"name":"stackoverflow"}'\'
or
read json_variable << \EOF
> '{"id":158,"name":"stackoverflow"}'
> EOF
I'm not sure why you need single quotes specifically in the output, but your root problem is that your assignment never adds the double quotes in the first place. First, quote the double quotes properly, using
json_variable='{"id":158,"name":"stackoverflow"}'
Then, add the single quotes to the echo command:
echo "'$json_variable'"
The same trick can be used to add single quotes to the value without worrying about complicated quoting schemes.
json_variable="'$json_variable'"

Special characters in script not working correctly

I am trying to write a script to change a password on multiple servers. The issue is that the password has special characters. This is the example of the echo command I am using:
echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
Here is the response:
[root#myserver ~]# echo -e "'P#7g$dkW$8Ej$5$%'\n'P#7g$dkW$8Ej$5$%'"
'P#7gEj$%'
'P#7gEj$%'
As you can see it is dropping a lot of characters. Any advice would be helpful.
Take out the double quotes and just use single quotes so variables (preceded by $) aren't expanded.
The reason why your string is interpreted that way is because you have used double-quotes to create the string. The single quote that you have set after that will be interpreted as a part of the string and has no special meaning anymore and doesn't make your password a literal string. That means that the password string is still in the scope of "" and $xyz will be interpreted as the xyz variable.
You can try to concatenate them like this
passwd1='password1'
passwd2='password2'
echo -e "$passwd1\n$passwd2"
or just leave out the double quotes in your solution.

Executing a command in a string in bash

I have a string that contains a command that I want to execute in a bash script. How can I do that?
Sorry for so basic question but I am new in bash.
This is my code:
echo "What is the path to save the result files?"
read out_path
end_cm1=$"fastqc -o "$out_path$" --noextract -fastq "$files1
And I want to execute the instruction that is in the end_cm1 variable.
First, you don't have to put that command in a string at all: you can just do this:
fastqc -o "$out_path" --noextract -fastq $files1
(And I'd recommend putting $out_path in quotes here in case the path has a space in it. I've not put $files1 in quotes because your variable is plural so I assume there's more than one; you should beware spaces in those file names also.)
Second, the answer to the question you asked is eval:
eval $end_cm1
You just have a slight syntax issue in your string:
end_cm1="fastqc -o $out_path --noextract -fastq $files1"
$enc_cm1
Having said that, #ams is right about not needing to assign this
to a string in the first place, and about the risks involved
in not quoting $files1.

Batch file setting double quote a variable

In a Windows XP .bat file, how do I set a variable value to use double quote? I couldn't find special characters for batch file.
SET myVariable= " \"myValue \" "
You can use the method #Patrick Cuff has offered or you can do it quite simply:
SET var="Value"
Let's see if it works...
ECHO %var%
and the output is:
"Value"
Yes! :)
If this is for Windows, you need to escape the double quotes with a caret (^):
set myVariable=^"myVlaue^"
Putting single quotes around the value won't work, the value will include the single and double quotes.
You can use
SET myVariable='"myValue"'

Problem with quoted filenames in Batch

Let I have a batch program:
SET FOO=C:\temp\%1
bar.exe %FOO%
When I call it with double quoted file name as an argument I get these quotes in the middle; and that fact prevents other programs from working correctly:
> fail.bat "aa bb.jpg"
SET FOO=C:\temp\"aa bb.jpg"
> bar.exe C:\temp\"aa bb.jpg"
cannot find file
How to get variable containing correct value "C:\temp\aa bb.jpg"?
You can use %~1 instead, this removes the quotes from the parameter.
Then your code should look like
SET FOO="C:\temp\%~1"
bar.exe %FOO%
Try removing the drive letter as I have had issues with that in the past. Also does it work if the entire path name is in quotes not just the single item with spaces?

Resources