What is the difference between % % and ! ! in a batch file? [duplicate] - windows

This question already has answers here:
Difference between %variable% and !variable! in batch file
(2 answers)
Windows batch file syntax using exclamation mark
(3 answers)
Example of delayed expansion in batch file
(5 answers)
Closed 1 year ago.
What is the difference between these 2 pieces of code in a batch file?
set str=!str:%%20= !
and
set str=%str:%%20= %

Finally, after hours I could find the explanation.
According to this page
How-to: Escape Characters, Delimiters and Quotes at the Windows command line in the paragraph "Escaping Percents" :
Escaping Exclamation marks
When the shell is running in EnableDelayedExpansion mode the ! character is used to denote a variable and so must be escaped (twice) if you wish to treat it as a regular character:
^^!

Related

Bash - echo changes order of output, replacing strings [duplicate]

This question already has answers here:
Are shell scripts sensitive to encoding and line endings?
(14 answers)
How to convert Windows end of line in Unix end of line (CR/LF to LF)
(8 answers)
Closed 29 days ago.
Trying to generate Wireguard key. And stuck with such behavior of echo command.
As you can see FINISH goes to the beginning and I can't understand how to avoid this.
#echo START$(wg genkey)FINISH
FINISH1111111111111111111111111111111111111111111
But in fact, the purpose of the command is to enclose the key in quotes.
With START/FINISH it’s clearer what happens.
#echo \"$(wg genkey)\"
"1111111111111111111111111111111111111111111
As a goal i need something like this, with existing quotes as part of an output.
"1111111111111111111111111111111111111111111"

What is the windows command line equivalent to what role "$" plays in a Linux terminal? [duplicate]

This question already has answers here:
Batch equivalent of Bash backticks
(5 answers)
How to set commands output as a variable in a batch file [duplicate]
(9 answers)
Closed 3 years ago.
As the question title states, I want to know what control character is used in the windows command line when we want all content within the proceeding brackets to be evaluated as the output one expects if that content were executed on a separate command line for example in Linux to assign a value to a variable:
variable=$(a valid command sequence);

Understand double quote with echo and shebang [duplicate]

This question already has answers here:
echo "#!" fails -- "event not found"
(5 answers)
! in a string makes it unusable in a command line - error message "event not found" [duplicate]
(1 answer)
-bash: !/usr/bin/env: event not found [duplicate]
(1 answer)
Closed 3 years ago.
I have a basic question about Shell interpretation
When i make something like :
echo "#!/bin/bash"
-bash: !/bin/bash: event not found
What does the shell really pass to the echo command.
If i understand well, the double quote make the shell doesnt do globbing.
! is used for history expansion, not for globbing.
From the bash documentation:
Enclosing characters in double quotes preserves the literal value of
all characters within the quotes, with the exception of $, `, \, and,
when history expansion is enabled, !.

Inserting Variable into sh Script Command [duplicate]

This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
#!/bin/sh -f
set proj_dir="OutputDir"
for projname in lib proj1 proj2
do
mv ./scripts/$projname_BYTECODE ./$proj_dir/scripts/$projname
done
A very simple example of what is not working well for me. $projname_BYTECODE is being interpreted as a variable name but _BYTECODE is actually part of the folder name. Suggestions?
Use ${X} instead of $X, so in your example ${projname}_BYTECODE should do the trick. Have a look at this question for more information: When do we need curly braces in variables using Bash?

convert backslashes to forward in batch files [duplicate]

This question already has an answer here:
Windows shell string operations (changing backslash to slash)
(1 answer)
Closed 9 years ago.
whats the easiest way of converting all backslashes to forward in a path in a batch file, since I need to use bash for execution.
SET "string=D:\path\to\folder"
ECHO %string:\=/%
Basically, you need first to store the string value into an environment variable, then use the following template:
%variable:str1=str2%
to replace every occurrence of str1 in variable with str2.
You can always remind yourself about this pattern by invoking SET /? from the command prompt.
echo 'C:\Program Files\Program' | sed -e 's/\\/\//g'

Resources