Bash Alias Command With Both Single and Double Quotes

bash alias command with both single and double quotes

You just need to escape it correctly.

alias xxx="svn status | awk '\$1 ==\"M\"{print \$2;}'"

Set alias with quotes and double quotes in command

A simple solution is to create a function, instead of an alias:

function function_name() {

expect -c 'spawn ssh usr@ip -p 57022 ; \
expect password ; send "pass\n" ; interact'

}

So you can call function_name, and it will work just as fine as with an alias.

If you still want to use an alias, just escape the inner "'s:

alias alias_name="expect -c 'spawn ssh usr@ip -p 57022 ; expect password ; send \"pass\n\" ; interact'"

and it should work.

How to escape single quotes as part of an alias composite command?

You have entered the realm where it no longer makes sense to use an alias - your code is just too big to be a one-liner.

The recommendation to use a function is a good one. Just replace your alias with:

candidate () {
cd $PROJECT_HOME || return
git checkout .
git clean -fd
export CREL_BRANCH=`git branch --list crel* | awk '{ print $NF }'`
git checkout $CREL_BRANCH
git pull
}

However, unless the CREL_BRANCH variable export is important to you I would recommend creating a script instead.

If you don't already have one, create a bin directory in your $HOME to store all your personal scripts:

mkdir ~/bin

Add this directory to your $PATH:

export PATH=$PATH:~/bin

Then create a file called candidate and make it executable:

touch ~/bin/candidate
chmod +x ~/bin/candidate

Edit the candidate file:

#! /bin/bash

cd $PROJECT_HOME || exit
git checkout .
git clean -fd
CREL_BRANCH=`git branch --list crel* | awk '{ print $NF }'`
git checkout $CREL_BRANCH
git pull

Scripts are much more maintainable and easier to modify. The only issue with scripts is that it runs in a subshell so you cannot export any variables from scripts.

If you need to export variables use a function. Still, I recommend to save that function in another file such as candidate.sh then in your bashrc add:

# Import candidate function (note the space after the dot!)
. /path/to/candidate.sh

How to escape single quotes within single quoted strings

If you really want to use single quotes in the outermost layer, remember that you can glue both kinds of quotation. Example:

 alias rxvt='urxvt -fg '"'"'#111111'"'"' -bg '"'"'#111111'"'"
# ^^^^^ ^^^^^ ^^^^^ ^^^^
# 12345 12345 12345 1234

Explanation of how '"'"' is interpreted as just ':

  1. ' End first quotation which uses single quotes.
  2. " Start second quotation, using double-quotes.
  3. ' Quoted character.
  4. " End second quotation, using double-quotes.
  5. ' Start third quotation, using single quotes.

If you do not place any whitespaces between (1) and (2), or between (4) and (5), the shell will interpret that string as a one long word.

Escaping Backslashes and Double Quotes in zsh Alias

It looks like you're treating the first and last double-quotes as 'surrounding' quotes for the entire expression, but that's not how it works in either zsh or bash. Instead, that's an expression consisting of a set of quoted and unquoted strings that are concatenated because they are adjacent.

A short example. This:

a=X b=Y c=Z
echo '$a'$b'$c'

will print this:

$aY$c

only the $a and $c are in single quotes, and are therefore not expanded.

Since some of the characters in your example (e.g. [, {) are not actually quoted, the shell attempts to expand them. It fails in zsh since the default behavior is to exit if a glob has no matches.

There are several ways to fix it.


Option 1 - make zsh behave like bash:

unsetopt nomatch
alias startdemoinstances="aws ssm start-automation-execution --document-name "AWS-StartEC2Instance" --document-version "\$DEFAULT" --target-parameter-name InstanceId --targets "[{"Key":"ResourceGroup","Values":["DemoInstances"]}]" --max-errors "1" --max-concurrency "1" --region ap-southeast-1"
setopt nomatch

This is not recommended. There are a lot of ways for it to go haywire, since we're counting on the shell ignoring special characters in an exact way.


Option 2 - escape internal double-quotes, so that the expression becomes one long string:

alias startdemoinstances="aws ssm start-automation-execution --document-name \"AWS-StartEC2Instance\" --document-version \"\$DEFAULT\" --target-parameter-name InstanceId --targets \"[{\"Key\":\"ResourceGroup\",\"Values\":[\"DemoInstances\"]}]\" --max-errors \"1\" --max-concurrency \"1\" --region ap-southeast-1"

This should also work in bash, and would be a very good idea there.


Option 3 - as @chepner suggested, use a much more readable function:

function startdemoinstances {
aws ssm start-automation-execution \
--document-name 'AWS-StartEC2Instance' \
--document-version "$DEFAULT" \
--target-parameter-name 'InstanceId' \
--targets '[{"Key":"ResourceGroup","Values":["DemoInstances"]}]' \
--max-errors '1' \
--max-concurrency '1' \
--region 'ap-southeast-1'
}

This should also work in bash.

Escaping a string with quotes in bash alias file

alias foo="my command with\"quotes\"" should work most of the time

EDIT: As bash will evaluate the string, you also need to escape other special characters like $

In your case: alias unlock="grep screen /var/log/auth.log|grep \"\$(date|awk '{print \$2\" \"\$3}')\"" should do the trick.

Escaping many layers of cascaded single and double quotes in osx bash

Assuming that you have a function, as opposed to an alias:

_sleep() { osascript -e 'tell application "Finder" to sleep' && exit; }

...you can emit its text with declare -f. Thus:

declare -f _sleep >>~/.profile

...or, to use your existing editprofile function:

editprofile "$(declare -f _sleep)"

The easiest approach is just that: Define the function in your local shell, then have the shell itself do the work of emitting it -- and quote that emitted content so it doesn't get field-split into individual arguments (and then have those arguments individually evaluated as globs).


If you don't want to go that route, there are approaches available; they're just varying degrees of unpleasant.

printf -v cmd_var '%q ' osascript -e 'tell application "Finder" to sleep' 

...will put correctly-quoted contents into "$cmd_var". You could then:

printf -v sleep_def '_sleep() { %s && exit; }' "$cmd_var"

...which will give you a function declaration in sleep_dev that can be evaled to execute it locally, or appended to your .profile, &c.

editprofile "$sleep_def"

...will behave appropriately in that context.



Related Topics



Leave a reply



Submit