Git Alias – Multiple Commands and Parameters
This will work (tested with zsh and bash): [alias] chs = !git checkout $1 && git status
This will work (tested with zsh and bash): [alias] chs = !git checkout $1 && git status
$@ is all of the parameters passed to the script. For instance, if you call ./someScript.sh foo bar then $@ will be equal to foo bar. If you do: ./someScript.sh foo bar and then inside someScript.sh reference: umbrella_corp_options “$@” this will be passed to umbrella_corp_options with each individual parameter enclosed in double quotes, allowing to … Read more
Add a “shebang” at the top of your file: #!/bin/bash And make your file executable (chmod +x script.sh). Finally, modify your path to add the directory where your script is located: export PATH=$PATH:/appropriate/directory (typically, you want $HOME/bin for storing your own scripts)
There are multiple versions of the echo command, with different behaviors. Apparently the shell used for your script uses a version that doesn’t recognize -n. The printf command has much more consistent behavior. echo is fine for simple things like echo hello, but I suggest using printf for anything more complicated. What system are you … Read more
Revised Answer (Feb 12, 2014) the_world_is_flat=true # …do something interesting… if [ “$the_world_is_flat” = true ] ; then echo ‘Be careful not to fall off!’ fi Original Answer Caveats: https://stackoverflow.com/a/21210966/89391 the_world_is_flat=true # …do something interesting… if $the_world_is_flat ; then echo ‘Be careful not to fall off!’ fi From: Using boolean variables in Bash The reason … Read more
In cross-platform, lowest-common-denominator sh you use: #!/bin/sh value=`cat config.txt` echo “$value” In bash or zsh, to read a whole file into a variable without invoking cat: #!/bin/bash value=$(<config.txt) echo “$value” Invoking cat in bash or zsh to slurp a file would be considered a Useless Use of Cat. Note that it is not necessary to … Read more
In all of the cases above, the variable is correctly set, but not correctly read! The right way is to use double quotes when referencing: echo “$var” This gives the expected value in all the examples given. Always quote variable references! Why? When a variable is unquoted, it will: Undergo field splitting where the value … Read more
You can also use parameter expansion: $ filename=foo.txt $ echo “${filename%.*}” foo Just be aware that if there is no file extension, it will look further back for dots, e.g. If the filename only starts with a dot (e.g. .bashrc) it will remove the whole filename. If there’s a dot only in the path (e.g. … Read more
For Bash scripts, the most direct and performant approach is: if compgen -G “${PROJECT_DIR}/*.png” > /dev/null; then echo “pattern exists!” fi This will work very speedily even in directories with millions of files and does not involve a new subshell. Source The simplest should be to rely on ls return value (it returns non-zero when … Read more
If you’re using Bash, the solution is to use $’string’, for example: $ STR=$’Hello\nWorld’ $ echo “$STR” # quotes are required here! Hello World If you’re using pretty much any other shell, just insert the newline as-is in the string: $ STR=’Hello > World’ Bash is pretty nice. It accepts more than just \n in … Read more