preface
As I worked for a long time, I found myself increasingly dependent on bash automation scripts. Bash’s many, many small aspects of our convenience add up to a flood of productivity improvements. So what’s the value of learning Bash? Bash has a history of more than thirty years and is one of the most powerful and portable tools available today for writing efficient scripts for all UNIX-based systems. So learning bash and learning re are both valuable things to do because they’re classic, useful, and timeless in the long run. Without further ado, this article is not a principle, but a tool to share ~ we can copy the application of practice, or by analogy.
precondition
All of the commands mentioned in this article were tested only on MacOS and were written to ~/.bash_profile and then source ~/.bash_profile to update the application. Bashrc. For example, I simply alias the commands that enable and compile bash_profile.
BASH_PROFILE_PATH="$HOME/.bash_profile"
alias bash.open="open ${BASH_PROFILE_PATH}"
alias bash.src="source ${BASH_PROFILE_PATH}"
Copy the code
Utility alias alias
Bash’s alias alias is arguably the feature I use the most, and it seems to me like a simple function that combines some cumbersome logic and exposes only an elegant alias.
Git series
In practice, git commands come and go. Why waste your time typing commands again and again when you know them by heart? Here are my common aliases.
alias g="git"
alias gb="git branch"
alias ga="git add"
alias gaa="git add ."
alias gap="git add -p"
alias gs="git status"
alias gco="git checkout"
alias gcp="git cherry-pick"
alias gcm="git commit -m"
alias gcma="git commit --amend"
alias gcl="git clone"
alias glo="git log"
alias glog="git log --graph --oneline"
alias glol="git log --oneline"
alias gdif="git diff HEAD^ HEAD"
Copy the code
Git projects can be viewed, submitted, compared, etc. on the command line in just a few characters.
Workflow series
Frequently opened applications and directories can be written as aliases to save time.
alias vscode='open -a /Applications/Visual\ Studio\ Code.app/'
alias cd.desk="cd ~/Desktop"
alias cd.byte="cd ~/Desktop/Bytedance"
alias cd.nginx="cd /usr/local/etc/nginx"
alias ip="ifconfig | grep -oE 'inet.*netmask' | grep -oE '(\d+\.) {3}\d+' | sed -n 2p"
Copy the code
You can also install the ‘code’ command in vscode.
code PROJECT
Copy the code
Tool series
# Fix `tree` chinese encode
alias tree="tree -N"
# Fix `stat` to default verbose
alias stat="stat -x"
# A nicer way to ls
alias ll="exa -lhaBgb --git"
# Copy pwd to clipboard
alias pwd.cp="pwd | pbcopy"
Copy the code
Note that the tree and exa commands need to be manually installed on MacOS.
brew install tree
brew install exa
Copy the code
The following tree effects:
Writing shell functions
Quickly create recursive folders and switch working paths
I don’t have to go into that. It’s quite practical.
mkcd () {
mkdir -p The $1 && cd ". /The $1"
}
Copy the code
Quick View of IP
You can quickly view the local IP address and automatically copy the IP address to the paste board.
alias ip="ifconfig | grep -oE 'inet.*netmask' | grep -oE '(\d+\.) {3}\d+' | sed -n 2p"
ipl () {
IP=$(ip)
echo $IP | pbcopy
echo "local IP address: $IP"
}
Copy the code
Quick view of disk space
Very fast.
ds () {
echo "Disk Space Utilization For $HOSTNAME"
df -h
}
Copy the code
Counting the number of Chinese characters
Ggrep needs to be installed manually.
countHan() {
ggrep -roP '[\p{Han}]' The $1
}
Copy the code
Regular switch Git branches
What if you don’t want to enter the full name of the branch when switching branches? How to be lazy? Switch by writing the re, of course.
gcof() {
REGEX=The $1
TARGET="$(g branch | grep -E $REGEX | grep -vx "\*.*" | head -n 1 | tr -d '[:space:]')"
if [[ -z $TARGET]].then
echo "Use regex '$REGEX' to match null"
else
echo "Use regex '$REGEX' to match branch '$TARGET'"
read -p "Are you sure to checkout branch '$TARGET' [Y/N]" -n 1 -s
echo # print blank line
if [[ $REPLY =~ ^[Yy]$ ]]; then
git checkout $TARGET
else
echo "Still on original branch"
fi
fi
}
Copy the code
I suggest you experience it, very elegant ~
Modify the command line prompt
Set the command line prompt in ~/.bash_profile or ~/.bashrc.
PS1='\ [0; 033 32 m \] 🤨 033 [0 m \] \ [\ \ w $'
export PS1
Copy the code
If someone asks me what’s the use of custom prompts? I’ll tell this student because it’s fun, of course.
The environment variable
Setting the environment variable vscode in VScode and then reading the variable in.bash_profile allows you to set different command line prompts based on this variable.
Profile file to read the environment variables
if [ -f ~/.profile ]; then
. ~/.profile
fi
Reset the prompt if the environment variables match
if [[ $VSCODE= ='sulirc@vscode']].then
echo "Ah choo~~ $USER, welcome to vscode terminal! (͡ ° ͜ ʖ ͡ °) ✧";
echo "";
PS1='\w $ '
fi
Copy the code
Convenient script collection
Quick Word search
grep -i '^c.. fu.. r$' /usr/share/dict/words
Copy the code
Quick File Clearing
Using redirected output to quickly empty files is very convenient
> filename.txt
Copy the code
Example Set the script execution permission
For script files, there are two common permission Settings; A script with permission 755 can be executed by anyone, and a script with permission 700 can be executed by only the file owner. Note that in order to be able to execute the script, it must be readable.
chmod 755 script.sh
Copy the code
Of course, you actually need to know more about chmod commands, such as ugo grouping, RWX permissions, etc., to use them in a fine-grained way.
Ignore standard output errors
Dear trash can /dev/null
$ non-exist-command 2> /dev/null
Copy the code
Vim configuration
Modify ~/. Vimrc to configure viM formatting
:syntax on
:set hlsearch
:set tabstop=4
:set autoindent
Copy the code
This allows syntax highlighting in Vim as well as automatic indentation when editing bash.
summary
Mastering the basics of Bash as well as looping, judging, functions, combined expressions, and regex doesn’t take a day or two. Practice, think, and consult your manual, and over time your skills will improve, and you’ll find that you’ll be able to read bash scripts that used to be magical. You can then automatically design new bash utility functions based on your own scenarios.
In addition, welcome to poke language finches address: www.yuque.com/sulirc/whal…
ITerm2 + ZSH + oh-my-zsh: iTerm2 + ZSH + oh-my-zsh: iTerm2 + ZSH