Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Lập Trình Bash

Nâng cao năng suất với 10 mẹo Bash Command Line thiết yếu

Học command line là thiết yếu cho bất kỳ developer nào.

Và để thực thi lệnh trên command line, bạn cần một shell.

Bash shell phổ biến trong Unix-like OS như Mac và Linux. Thực tế hầu hết Linux distros, Bash là default shell.

Bạn cũng dùng Bash trong Windows qua WSL (Windows Subsystem for Linux).

Sau khi học basic Bash commands, đến lúc speed up.

Tutorial này không cho absolute beginners, nhưng hy vọng cả novice và advanced users đềuได้ benefit.

Đây là 10 Bash commands giúp bạn làm việc nhanh hơn với terminal.

1. Dùng Control + L clear screen và Control + D để exit

Clear terminal screen, gõ clear trên command line.

Để exit, gõ exit.

Tốt hơn, nhấn Ctrl + l ( ⌘ + l ) clear screen và Ctrl + d (⌘ + d) đóng terminal.

2. Dùng lệnh nohup spawn processes không kết thúc khi terminal session kết thúc

Khi programs loaded vào memory, chúng gọi là processes.

Thỉnh thoảng tôi mở Firefox từ command line:
firefox https://freecodecamp.org.

Nhưng ngay khi đóng terminal, Firefox cũng crash.

Để prevent this, dùng nohup (no hang up): nohup firefox https://freecodecamp.org.

Bây giờ khi đóng terminal, Firefox không crash nhưng tab crash.

Fix: làm Firefox background process bằng append symbol &.

nohup firefox https://freecodecamp.org &

Bây giờ ngay cả khi quit terminal, tabs đều intact.

3. Dùng pkill kill processes bằng cách gõ chỉ một phần tên

Dùng killall kill process by name:

killall firefox

Tốt hơn, dùng pkill terminate bằng gõ chỉ một phần tên.

pkill fire*

4. Prepend time command biết execution time

Muốn biết command thực thi bao lâu?

Chỉ prepend time vào command: time gcc -g *.c.

5. Trên Linux, dùng cat /etc/*rel* xem distro name

Gõ uname -a show system information.

Muốn double check distro đang chạy?

Chỉ gõ cat /etc/*rel* trên shell và enter.

6. Dùng sed trong text files find and replace

Muốn replace multiple occurrences từ trong text file?

Dùng sed command.

sed s'/apples/oranges/g' myfile.txt

Ở đây, tất cả occurrences của 'apples' đổi thành 'oranges'.

Nếu chỉ replace first occurrence mỗi line, bỏ 'g' suffix: sed s'/apples/oranges/' myfile.txt.

'g' là global.

Forward slash / là delimiter. Thực tế bạn dùng any delimiter.

Dùng single underscore _ làm delimiter: sed s'_apples_oranges_'g ` myfile.txt.

Chỉ dùng sed chỉ replace trên standard output. File gốc unchanged.

Để change file 'in place', dùng flag -i: sed -i s'_apples_oranges_g' myfile.txt.

7. Biết public IP address máy tính dùng curl

Có hai loại IP: private và public.

System assign internal IP check bằng ifconfig.

Nhưng muốn biết public IP máy tính – IP ISP assign cho interface?

Khi online, dùng curl ifconfig.me ; echo hoặc curl ifconfig.co ; echo trên command line.

Nhấn 'up' arrow key show last command typed.

Gõ history show all commands stored in bash history.

Tốt hơn, gõ Ctrl + r (⌘ + r) trên shell và bắt đầu gõ command.

Khi gõ, shell autocomplete từ history. Nhấn 'enter' ngay khi tìm match.

Nếu chỉ nhớ một điều từ tutorial này, nhớ key combination: Ctrl + r (⌘ + r).

Sẽ save bạn nhiều thời gian, guaranteed!

9. Dùng shell làm math

Simple calculations không input/output fractions, dùng:

:~$ echo $((19*34))
:~$ 646

Calculations involve fractions, echo expression pipe vào bc command.

:~$ echo "scale=2; 9*3/((2*2)+1)" | bc
:~$ 5.40

Ở đây, 'scale' là precision cần. Tôi set 2 decimal points.

10. Dùng brace expansion tạo files in bulk

Tạo 100 files trong folder như thế nào?

file1.txt, file2.txt, file3.txt ... file100.txt

Dùng brace expansion: touch file{1..100}.txt.

Cần tạo ba files cho project: app.html, app.css, app.js

Thay vì tạo từng cái, dùng brace expansion tạo all at once.

:~$ touch app.{html,css,js}
:~$ ls
app.html app.css app.js
:~$

Hoặc trong project folder, cần tạo năm directories: images, css, src, templates, scripts.

Dùng:

:~$ mkdir {images,css,src,templates,scripts}
:~$ ls
images css src templates scripts
:~$

Chỉ một caveat: ensure no spaces giữa words inside braces.

Wrapping up

Tôi listed 10 Bash command line tips giúp bạn work up to speed trên terminal.

Học các Bash commands này và sẽ hold you in good stead trong programming journey.

Happy Coding!