variable-expansion

How do I echo $command without breaking the layout

孤者浪人 提交于 2019-11-28 05:28:29
问题 I'm trying to do the following in a bash script: com=`ssh host "ls -lh"` echo $com It works, but the echo will break the output (instead of getting all lines in a column, I get them all in a row). If I do: ssh host ls -lh in the CLI it will give me the correct output and layout. How can I preserve the layout when echoing a variable? 回答1: You need: echo "$com" The quotes make the shell not break the value up into "words", but pass it as a single argument to echo . 回答2: Put double quotes around

Expand variable inside single quotes

耗尽温柔 提交于 2019-11-27 15:07:16
How can I expand $pw inside single quotes? $pw = "$PsHome\powershell.exe" cmd.exe /c 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' You can use formatting and assign it to another variable: $pw = "$PsHome\powershell.exe"; $command = 'schtasks /create /tn cleanup /tr "{0} -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' -f $pw; cmd.exe /c $command Or you can use double quotes and escape the inside quotes with quotes: $pw = "$PsHome\powershell.exe" cmd.exe /c

bash quotes in variable treated different when expanded to command

心已入冬 提交于 2019-11-27 14:32:36
Explaining the question through examples... Demonstrates that the single-quotes after --chapters is gets escaped when the variable is expanded (I didn't expect this): prompt@ubuntu:/my/scripts$ cat test1.sh #!/bin/bash actions="--tags all:" actions+=" --chapters ''" mkvpropedit "$1" $actions prompt@ubuntu:/my/scripts$ ./test1.sh some.mkv Error: Could not open '''' for reading. And now for some reason mkvpropedit receives the double quotes as part of the filename (I didn't expect this either): prompt@ubuntu:/my/scripts$ cat test1x.sh #!/bin/bash command="mkvpropedit \"$1\"" command+=" --tags

Is there a way to prevent percent expansion of env variable in Windows command line?

白昼怎懂夜的黑 提交于 2019-11-27 07:16:22
问题 I'm using the following git command in git bash on Windows: git log --format="%C(cyan)%cd%Creset %s" --date=short -5 It displays commit date ( %cd ) followed by commit message ( %s ). Commit date is wrapped with color markers: %C(cyan) to start colored output and %Creset to stop colored output. While it works fine in git bash, it doesn't do well with cmd : %cd% is expanded by Windows shell into current working directory (equivalent of $PWD in bash). Hence when that command is run via cmd , I

Word splitting in Bash with IFS set to a non-whitespace character

蹲街弑〆低调 提交于 2019-11-27 06:13:42
问题 I'm going through a Bash tutorial, and specifically the subject of word splitting. This script, called "args", helps demonstrate word splitting examples: #!/usr/bin/env bash printf "%d args:" $# printf " <%s>" "$@" echo An example: $ ./args hello world, here is "a string of text!" 5 args: <hello> <world,> <here> <is> <a string of text!> So far so good. I understand how this works. However, when I replace IFS with a non-whitespace character, say : , the script does not perform word splitting

What is the most elegant way to remove a path from the $PATH variable in Bash?

北城以北 提交于 2019-11-26 21:23:42
Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable? I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I've lost track of it. A quick search of Google turned up surprisingly few relevant results and none that I would call "simple" or "elegant". For example, two methods using sed and awk, respectively: PATH=$(echo $PATH | sed -e 's;:\?/home/user/bin;;' -e 's;/home/user/bin:\?;;') PATH=!(awk -F: '{for(i=1;i<=NF;i++){if(!($i in a)){a[$i];printf s$i;s=":"}}}'<<<$PATH) Does

Expand variable inside single quotes

半世苍凉 提交于 2019-11-26 18:28:20
问题 How can I expand $pw inside single quotes? $pw = "$PsHome\powershell.exe" cmd.exe /c 'schtasks /create /tn cleanup /tr "$pw -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' 回答1: You can use formatting and assign it to another variable: $pw = "$PsHome\powershell.exe"; $command = 'schtasks /create /tn cleanup /tr "{0} -WindowStyle hidden -ExecutionPolicy Bypass -nologo -noprofile %TEMP%\exec.ps1" /sc minute /mo 1' -f $pw; cmd.exe /c $command Or

bash quotes in variable treated different when expanded to command [duplicate]

纵饮孤独 提交于 2019-11-26 16:47:51
问题 This question already has an answer here: Why does shell ignore quotes in arguments passed to it through variables? [duplicate] 3 answers Explaining the question through examples... Demonstrates that the single-quotes after --chapters is gets escaped when the variable is expanded (I didn't expect this): prompt@ubuntu:/my/scripts$ cat test1.sh #!/bin/bash actions="--tags all:" actions+=" --chapters ''" mkvpropedit "$1" $actions prompt@ubuntu:/my/scripts$ ./test1.sh some.mkv Error: Could not

What is the most elegant way to remove a path from the $PATH variable in Bash?

橙三吉。 提交于 2019-11-26 12:18:03
问题 Or more generally, how do I remove an item from a colon-separated list in a Bash environment variable? I thought I had seen a simple way to do this years ago, using the more advanced forms of Bash variable expansion, but if so I\'ve lost track of it. A quick search of Google turned up surprisingly few relevant results and none that I would call \"simple\" or \"elegant\". For example, two methods using sed and awk, respectively: PATH=$(echo $PATH | sed -e \'s;:\\?/home/user/bin;;\' -e \'s;

Brace expansion with variable? [duplicate]

China☆狼群 提交于 2019-11-25 22:47:46
问题 This question already has answers here : How do I iterate over a range of numbers defined by variables in Bash? (20 answers) Variables in bash seq replacement ({1..10}) [duplicate] (7 answers) Closed 2 years ago . #!/bin/sh for i in {1..5} do echo \"Welcome\" done Would work, displays Welcome 5 times. #!/bin/sh howmany=`grep -c $1 /root/file` for i in {1..$howmany} do echo \"Welcome\" done Doesn\'t work! howmany would equal 5 as that is what the output of grep -c would display. $1 is