Manipulating variables in windows - windows

I know there are a few things you can do directly with a variable, such as cut off ends of the variables via %var:~0,4%, or even do character replacement via %var:/=-%. What are these features called? And does anyone have a link to documentation for them?

It's the old dos string manipulation. See http://www.dostips.com/DtTipsStringManipulation.php

I would say "Environment variable substitution" as described in SET /? (although this omits to mention parameter substitution commands like %~nxN for extracting a file name from a path passed as the Nth command line argument).

Related

Difference between ${my_variable} and $my_variable in a Jenkins project

Is there a difference when using variables in a Jenkins project between this:
node index.js ${arg}
and this:
node index.js $arg
Where arg is a parameter for the project.
Update:
Interesting to note that it's not Jenkins-specific.
I think this question should remain as others may assume it's something to do with Jenkins.
In the context of your Jenkins project, it is not needed, except as a matter of style.
The braces are useful in those cases where the shell may not be able to determine the end of a variable name. For instance, if your variable is named this, then you would need the braces if your command was
echo "${this}isatest"
Also, you need them when you want to take advantage of Bash's Shell Parameter Expansions.
It's actually a standard shell syntax.
It's easier to manipulate variables / concatenate the contents of variables into other variable names. e.g.
${foo}bar
You can also perform additional string manipulation with the {}:
STRING="This is a string"
echo ${STRING// /_}
http://www.tldp.org/LDP/abs/html/string-manipulation.html
I also find variables with {} to read better but that's a personal preference.
Generic answer here: When do we need curly braces in variables using Bash?

How to pass multiple variables in shell script

I have a shell script that I'm trying to write to a file using multiple variables, but one of them is being ignored.
#!/bin/bash
dir=/folder
name=bob
date=`date +%Y`
command > $dir/$name_$date.ext
The $name is being ignored. How can I fix this?
Have you noticed that the _ was "ignored" as well? That's a big hint.
If you use set -u, you'll see the following:
-bash: name_: unbound variable
The way bash parses it, the underscore is part of the variable name.
There are several ways to fix the problem.
The cleanest is the ${var} construct which separate the variable name from its surroundings.
You can also use quotation in various ways to force the right parsing, e.g.: "$dir/$name""_$date.ext"
And in case your variables might contain spaces (now, or in the future) use quotation for words.
command >"$dir/${name}_$date.ext"
command >"${dir}/${name}_${date}.ext"
Both these are fine, just pick one style and stick to it.

Insert current path in bash command line

To run a program in bash, I normally use relative paths because it's faster to type; for example, something like
me#host:~/dir/appX$ ./manage.py runserver
The command will then be stored in the history. To recall the command from history (CTRL+R normally), I need to be on the same path as when I ran it, making the recall function less useful.
One solution is to insert the full path the first time, but it takes a lot of writing.
me#host:~/dir/appX$ /home/me/dir/appX/manage.py runserver
Is there a way (preferably built in) to insert the current path in the command line?
Or maybe a better solution (should work on bash)?
You can do this in bash using Tilde Expansion. You need two tilde expansion related features, just showing the relevant parts from man bash below:
Tilde Expansion
If the tilde-prefix is a `~+', the value of the shell variable PWD
replaces the tilde-prefix.
tilde-expand (M-&)
Perform tilde expansion on the current word.
As it says, you can type ~+ to get the current path. And then to expand it you need to type M-&. So the key sequence ~+M-& is all you need.
I found it a little difficult pressing all these keys, so I created a key binding for this. Add a line like below in your ~/.inputrc file:
"\C-a":"~+\e&"
With this I can now type ctrl+a on my keyboard to get the current path on the command line.
PS: It's possible that ctrl+a is already bound to something else (perhaps beginning of line) in which case it might be better to use another key combination. Use bind -p to check current bindings.

How can I trigger brace expansion inside a script?

I'm writing a script which needs to use the shell's brace expansion, but nothing I've tried works. For (a contrived) instance, say I have a variable containing the string
thing{01..02}
and I (obviously) want to expand it to
thing01 thing02
from inside the script, how can I do that?
(For anyone who assumes this is a duplicate of this other question, please read them more carefully. That question is regarding working from the shell, not a shell script, and doesn't require the ability to expand arbitrary expressions.)
$ echo thing{01,02}
thing01 thing02
Make sure that braceexpand is turned on with set -o braceexpand.

Windows Command Line: Non Evaluation of Environment Variable

I would like to provide the raw text referring to an environment variable to a command instead of evaluating the environment variable.
I need this to configure BizTalk from the command line, for example:
BTSTask.exe AddResource -ApplicationName:App1
-Type:System.BizTalk:BizTalkAssembly -Overwrite
-Source:..\Schemas\bin\development\App1.Schemas.dll
-Destination:%BTAD_InstallDir%\App1.Schemas.dll
This command adds a resource to a BizTalk application. I want the destination to be %BTAD_InstallDir%\App1.Schemas.dll, however at present it is evaluating the environment variable (to nothing) and using \App1.Schemas.dll.
Is it possible to escape or disable the evaluation of this environment variable while parsing\executing this command?
I have tried escaping the first and both percentage characters with a carrot (^), however this did not stop the evaluation.
[EDIT] When I execute this at the command prompt it doesn't replace the environment variable, however it does when I run it as a script, any thoughts as to why this is different?
Try echo ^%path^% in a command prompt it prints...
path
instead of expanding the environment variable so I guess the following should work for you as suggested by Mikeage
BTSTask.exe AddResource -ApplicationName:App1 -Type:System.BizTalk:BizTalkAssembly -Overwrite -Source:..\Schemas\bin\development\App1.Schemas.dll -Destination:^%BTAD_InstallDir^%\App1.Schemas.dll
Did you try:
%%BTAD_InstallDir%%
in your script ?
That should prevent the script to interpret the variable, and it would pass %BTAD_InstallDir% to the program.
Try ^% instead of %.
Tried:
C:\PrgCmdLine\Unix\echo.exe "%"JAVA_HOME"%"
Got:
%JAVA_HOME%
[EDIT] Indeed, C:\PrgCmdLine\Unix\echo.exe ^%JAVA_HOME^% works too, and is simpler...
[EDIT 2] For the record: I used UnxUtils' echo to have the behavior of a plain program. Built in echo has a slightly different behavior, at least for quoted % signs.
Not sure if it's the same as my case, but i was troubling to use a batch file to create a script which has %temp% variable inside.
The workaround i found:
set test=%temp;
echo {command} %test%%>>path_to_my_batch_file;
Hope this helps someone:)

Resources