- Bash Cookbook
- Ron Brash Ganesh Naik
- 530字
- 2021-07-23 19:17:28
Your first Bash script with Vim
Let's start by creating a script using improved version of vi (called vim). If Vim (VI-enhanced) is not installed, it can be installed with sudo or root using the following command (-y is short for yes):
For Ubuntu or Debian based distributions
$ sudo apt-get -y install vim
For CentOS or RHEL
$ sudo yum install -y vim
For Fedora
$ sudo dnf install -y vim
Open a terminal and enter the following commands to first see where your terminal is currently navigated to, and to create the script using vim:
$ pwd
/home/yourUserName
$ vim my_first_script.sh
The terminal window will transform into the Vim application (similar to the following screenshot) and you will be just about ready to program your first script. Simultaneously press the Esc+ I keys to enter Insert mode; there will be an indicator in the bottom left and the cursor block will begin to flash:

To navigate Vim, you may use any number of keyboard shortcuts, but the arrow keys are the simplest to move the cursor up, down, left, and right. Move the cursor to the beginning of the first line and type the following:
#!/bin/bash
# Echo this is my first comment
echo "Hello world! This is my first Bash script!"
echo -n "I am executing the script with user: "
whoami
echo -n "I am currently running in the directory: "
pwd
exit 0
We have already introduced the concept of a comment and a few basic commands, but we have yet to introduce the flexible echo command. The echo command can be used to print text to the console or into files, and the -n flag prints text without the end line character (end line has the same effect as pressing Enter on the keyboard)—this allows the output from the whoami and pwd commands to appear on the same line.
The program also exits with a status of 0, which means that it exited with a normal status. This will be covered later as we move toward searching or checking command exit statuses for errors and other conditions.
When you've finished, press Esc to exit insert mode; going back to command mode and typing : will allow you to write the vim command w + q. In summary, type the following key sequence: Esc and then :wq. This will exit Vim by writing to disk (w) and quitting (q), and will return you to the console.
To execute your first script, enter the bash my_first_script.sh command and the console will return a similar output:
$ bash my_first_script.sh
Hello world! This is my first Bash script!
I am executing the script with user: rbrash
I am currently running in the directory: /home/rbrash
$
Congratulations—you have created and executed your first Bash script. With these skills, you can begin creating more complex scripts to automate and simplify just about any daily CLI routines.
- 零基礎(chǔ)搭建量化投資系統(tǒng):以Python為工具
- Vue.js前端開發(fā)基礎(chǔ)與項(xiàng)目實(shí)戰(zhàn)
- Microsoft Dynamics 365 Extensions Cookbook
- Spring Boot+Spring Cloud+Vue+Element項(xiàng)目實(shí)戰(zhàn):手把手教你開發(fā)權(quán)限管理系統(tǒng)
- Cassandra Design Patterns(Second Edition)
- 網(wǎng)頁(yè)設(shè)計(jì)與制作教程(HTML+CSS+JavaScript)(第2版)
- 從0到1:HTML+CSS快速上手
- PhoneGap Mobile Application Development Cookbook
- Visual C++應(yīng)用開發(fā)
- Python數(shù)據(jù)分析從0到1
- 小程序開發(fā)原理與實(shí)戰(zhàn)
- 焊接機(jī)器人系統(tǒng)操作、編程與維護(hù)
- Qlik Sense? Cookbook
- 零基礎(chǔ)學(xué)Scratch 3.0編程
- Visual Basic程序設(shè)計(jì)全程指南