things you should know about the Bourne Again Shell (bash)

Bash.md 10KB

Stuff you should know about bash

Bash

  • Original author(s) Brian Fox
  • Initial release June 8, 1989; 28 years ago
  • Stable release 4.4.18 / January 30, 2018; 56 days ago[1]
  • Repository https://git.savannah.gnu.org/cgit/bash.git
  • Wikipedia Bash_(Unix_shell)
  • Written in C
  • Operating system Unix-like, macOS (system includes an older GNU GPL v2 version); current version available through third parties, Windows (newer GNU GPL v3+ version)
  • Platform GNU
  • Available in Multilingual (gettext)
  • Type Unix shell, command language
  • License GNU GPL v3+[6]
  • Website www.gnu.org/software/bash/

Bash is a Unix shell and command language written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell. First released in 1989, it has been distributed widely as the default login shell for most Linux distributions and Apple's macOS (formerly OS X). A version is also available for Windows 10.

Bash is a command processor that typically runs in a text window, where the user types commands that cause actions. Bash can also read and execute commands from a file, called a shell script. Like all Unix shells, it supports filename globbing (wildcard matching), piping, here documents, command substitution, variables, and control structures for condition-testing and iteration. The keywords, syntax and other basic features of the language are all copied from sh. Other features, e.g., history, are copied from csh and ksh. Bash is a POSIX-compliant shell, but with a number of extensions.

The shell's name is an acronym for Bourne-again shell, a pun on the name of the Bourne shell that it replaces and on the common term "born again".

Top Ten (plus a few) shell commands

pwd

pwd is Print Working Directory. What else can be said about the good old pwd who has been printing the current directory name for ages. Prints out where in the filesystem you are currently.

ls

Lists the folder and file names in the current working directory. You can narrow the list down to files matching a specific name by passing the name as a parameter. This comes with a variety of flags; a few key ones are -l to list the long format that displays the file permissions, and -a to list all files including hidden files.

So now you can see your files and folders, to navigate to a different directory you use…

cd

Change Directory to the path specified, for example cd projects. There are a few really helpful arguments to aid this:

. refers to the current directory ./projects .. can be used to move up one folder use cd .., and can be combined to move up multiple levels ../../my_folder /is the root of your system to reach core folders, such as system, users, etc ~ is the home directory, usually the path /users/username. Move back to folders referenced relative to this path by including it at the start of your path, for example ~/projects . If you need to access your shell configuration, you can easily find this with ~/.bashrc or ~/.zshrc — then add all sorts of beneficial aliases, configurations, commands and paths. For more on the mac directory structure

  • OSXDaily: Mac OS X Directory Structure Explained Now you can navigate directories, its time to create some of your own using…

mkdir

Make directories with this command mkdir my_folder . Not only can it make the folder specified, but also its parents if they do not exist already using the -p option. The command mkdir -p first_folder/next_folder/my_folder would also create the first and next folder. There is an option to set the mode, or permissions, via the -m flag, and these can be changed later with the chmod command (see below for more on mode and permissions).

There are various ways to then create files using the command line, one of the most common is…

touch

Touches the file to update the access and or modification date of a file or directory without opening, saving or closing the file. But one of the most common uses is to create an empty file touch my_file.

Before you ask why I suggested touch to create files, read this page, which explores ways to create files using shell commands

  • StackExchange Unix & Linux: Why isn’t there any shell command to create files? To then use the file, open my_file will launch the application set for this file type. But you might want to see what’s in your files within the scope of the terminal. To do this you can use…

cat

Concatenate and print files to stdout cat my_file. You can pass one or more file names to this command, and even number the lines using the -n to number the lines.

For more on ways to use the cat command

  • 13 Basic Cat Command Examples in Linux The files may not be in the place you want them to be, rather than drag and drop using the mouse and windows, you command your terminal to…

mv

Moves files and folders. The first argument is the file you want to move, and the second is the location to move it to. Use the flags -f to force move them and -i to prompt confirmation before overwriting files.

Alternatively, you may not want to remove the original, for example when making backups, in this case you would use…

cp

Copies files and folders cp my_file ./projects . The flag -r recursively copies subfolders and files.

Now you have too many files, or some of them are no longer needed. The next command is a danger zone, because it is so good at its job…

rm

Removes files and folders rm my_folder . Using -r will again recursively delete subfolders, -f force deletes, and -rf for a recursive force delete. If you want to remove all folders and files in the current directory the command is rm -rf ./* , if you leave out the dot then it would reference the root directory!

Want to see what happens if you force a computer to run rm -rf /? Tip: don’t try this on yours, instead watch this video:

  • What happens when you sudo rm -rf / your machine? — YouTube There are ways to stop accidental deletes. If you use the -i flag (for interactive) you can specify the computer to prompt confirmation before delete.

Another way is with permissions, which can be modified…

chmod

Change mode so you can set permissions for read, write and execute for the user, members of your group and others. This uses binary values as an argument to set these. There are many common chmod permissions, a few key ones are:

777 — anyone can read, write and execute chmod 777 my_file 755 — for files that should be readable and executable by others, but only changeable by the issuing user 700 — only the user/owner can do anything to the file For a more lengthy and detailed explanations of chmod see man chmod

chown

Change owner is used to set which user owns a file or directory. Often you need to run sudo to make chown work. chown is a command to change the ownership of a file/folder or even multiple files/folders for a specified user/group.

man

Manuals for a command can be shown with this instruction. Below is some of the output from running man ls, it also displays all the options available for running the command. This can be very important when you need to learn about the weird flags you might not know anything about. Try man man?

LS(1)                     BSD General Commands Manual                    LS(1)
NAME
     ls -- list directory contents
SYNOPSIS
     ls [-ABCFGHLOPRSTUW@abcdefghiklmnopqrstuwx1] [file ...]
DESCRIPTION
     For each operand that names a file of a type other than directory, ls displays its name as well as
     any requested, associated information.  For each operand that names a file of type directory, ls
     displays the names of files contained within that directory, as well as any requested, associated
     information.
     
     ...trimmed for example...

env

When you type env, you'll get a listing of all the shell's current environment variables.

$ env
TERM_PROGRAM=Apple_Terminal
SHELL=/bin/bash
TERM=xterm-256color
TMPDIR=/var/folders/2r/h0wd6frs6mjg7svdz6xkxchc0000gn/T/
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.SnQXyNrJvs/Render
PLAN9=/Users/jj/GitHub/plan9port
TERM_PROGRAM_VERSION=400
TERM_SESSION_ID=73B19B26-443A-4DA6-AB30-C2AF953168E4
USER=jj
SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.WrZJikIm6Z/Listeners
PATH=/Users/jj/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/go/bin
PWD=/Users/kristofer
LANG=en_US.UTF-8
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
SHLVL=1
HOME=/Users/jj
GOPROJ=/Users/jj/GitHub/go/src/github.com/ggiti/
LOGNAME=jj
GOPATH=/Users/jj/GitHub/go/
SECURITYSESSIONID=186a7
_=/usr/bin/env

you can use echo to see a particular setting in the shell, echo $TERM would print something like xterm-256color.

df & du (disk commands)

Running out of disk space is a gradual process that might take years, but can still catch you off guard. There are two commands you can use to check your free space and determine which files are hogging your disk: du (disk usage) and df (disk free). They both take a -h option (human readable). To get a sense of how full your disk is, use the df command:

$ df -h
Filesystem                                          Size   Used  Avail Capacity iused               ifree %iused  Mounted on
/dev/disk1s1                                       234Gi   61Gi  167Gi    27% 1514122 9223372036853261685    0%   /
devfs                                              198Ki  198Ki    0Bi   100%     689                   0  100%   /dev
/dev/disk1s4                                       234Gi  5.0Gi  167Gi     3%       5 9223372036854775802    0%   /private/var/vm
map -hosts                                           0Bi    0Bi    0Bi   100%       0                   0  100%   /net
map auto_home                                        0Bi    0Bi    0Bi   100%       0                   0  100%   /home

and du command:

$ du -h -s *
  0B	#.bashrc#
1.6M	Answers
  0B	Applications
106M	Desktop
311M	Documents
1.5G	Downloads
2.2G	GitHub
 72K	IdeaProjects
 15G	Library
  0B	Movies
  0B	Music
 14M	PageRank-Page-Brin.pdf
 32M	Pictures
  0B	Public
220K	UNIX-Ritchie-Thompson.pdf
112K	UnixAsApp-Mach.pdf
1.1M	vonnuemann-edvac.pdf
  0B	zipcity.php?inputstring=19707

which shows how much space each item in a home directory is taking on the disk.