Skip to content

Commit

Permalink
Fixed typo in Unix tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
kah3f committed Aug 31, 2023
1 parent 24c5fa2 commit 09b120e
Show file tree
Hide file tree
Showing 2 changed files with 145 additions and 145 deletions.
145 changes: 96 additions & 49 deletions content/notes/unix-tutorial/unix_tutorial_6.md
Original file line number Diff line number Diff line change
@@ -1,104 +1,151 @@
---
title: "Environment Variables"
linktitle: "Tutorial 7: Environment Variables"
title: "Other Useful Commands"
linktitle: "Tutorial 6: Other Useful Commands"
date: 2019-04-29T11:06:47-04:00
draft: false
highlight_style: "github"
toc: true
type: docs
weight: 80
weight: 70

menu:
unix-tutorials:
---

Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program. Standard Unix variables are split into two categories, _environment variables_ and _shell variables_. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions. Environment variables are exported and have a farther reaching significance; those set at login are valid for the duration of the session. By convention, environment variables are written in UPPERCASE while shell variables usually have lowercase names.
### `exit`
Exit the current shell. If it is the login shell, this command logs the user off.

## Environment Variables
### `which`
The `which` command indicates the path to the executable specified.
```bash
% which myexec
```
The which command returns the location of the executable according to the rules used to search paths. The shell always searches from left to right in the list contained in the PATH environment variable; the first executable of the specified name is the one that is used.

### `wc`
The `wc` command returns the number of lines, words, and characters in an ASCII file. A word is defined as a non-zero length string surrounded by whitespace.
```
% wc myfile.txt
```

An example of an environment variable is the `$SHELL` variable. The value of this is the current shell you are using. Type
To print only the number of lines, use
```bash
% echo $SHELL
% wc –l myfile.txt
```

More examples of environment variables are
### `diff`
`diff` shows the differences between two ASCII files on a per-line basis.
```bash
$USER (your login name)
$HOME (the path name of your home directory)
$PWD (current working directory)
$DISPLAY (the name of the computer screen to display X windows; only set if X is enabled)
$PATH (the directories the shell should search to find a command)
% diff file1 file2
```

## Using and Setting Variables
### `find`
`find` is an extremely powerful command with many options. The simplest and most common use of it is to search for a file of a given name or with a name containing a pattern.
```bash
% find . -name myscript.sh
```
This starts from current directory (`.`) and searches for myscript.sh. The period is optional under Linux (but not under Mac OSX). To search for a name with a pattern it must typically be enclosed in quotes
```bash
% find . -name "*.sh"
```

Environment variables are set using the `export` command (`bash` or `zsh`) or the `setenv` command (`tcsh` or `csh`), displayed using the `printenv` (`bash`, `tcsh`) or `env` (`bash`, `zsh`) commands, and unset using the `unsetenv` command. To show all values of these variables, type
See the manpage or examples online for more usage patterns of this command.

### `du`
The `du` command outputs the number of kilobyes used by each subdirectory. Useful if you have gone over quota and you want to find out which directory has the most files. Some options make it more useful; in particular, -s summarizes directories and -h prints it in human-readable format. In your home directory, type
```bash
% printenv | more
% du -s -h *
```

To set a value of an environment variable, type (for `bash`)
### `gzip` and `zip`

This reduces the size of a file, thus freeing valuable disk space. For example, type
```bash
% export VAR=value
% ls -l science.txt
```
and note the size of the file. Then to compress `science.txt`, type
```bash
% gzip science.txt
```

## Sourcing
A group of shell commands can be placed into a file and then _sourced_. When a file is sourced, the commands are executed as if they had been typed at the command line in the current shell. For example, if several environment variables needed to be set over and over again, they could be collected into a file such as
This will compress the file and place it in a file called science.txt.gz. To see the change in size, type ls -l again. To uncompress the file, use the gunzip command.
```
% gunzip science.txt.gz
```

{{code-download file="/notes/unix-tutorial/snippets/envs.sh" lang="bash" >}}
Most Linux systems also provide the standard `zip` and `unzip` commands.
```bash
% zip science.txt.zip
% unzip science.txt.zip
```

## Exercise 6A
### tar (tape archive)

Download the envs.sh file to the Unix system you are using. Run the command
The standard archive format in Linux is `tar`. Tar is usually used to bundle directories. (Zip can also be used for this purpose.) Typically the output file is compressed with gzip.
```bash
source envs.sh
tar czf mydir.tar.gz mydir
```
The `c` option creates the tarfile (also called a "tarball"). The `f` option is for file, and this form of the command must be followed by the name of the file to contain the archive. The `z` option gzips the file.

Print the values of the environment variables in the file. To print the value of the shell variable ncpus, type
To extract the files
```bash
echo $ncpus
tar xf mydir.tar.gz
```
Newer versions of `tar` can detect that the archive is zipped, so a `z` is not necessary for extraction. The `x` option extracts. This will create the directory `mydir` if it does not exist. If it does, the contents will be replaced by the contents of mydir.tar.gz.

## Dotfiles
### `file`

Each time you log in to a Unix host, the system looks in your home directory for initialization files. Information in these files is used to set up your working environment. The first initialization file sourced is the _login_ initialization. It is sourced only in the login shell. Note: modern "desktop" user interfaces tend to "swallow" the login setup file, and it may be difficult to determine what is happening in these cases if there is an error.
`file` classifies the named files according to the type of data they contain, for example ASCII (text), pictures, compressed data, etc. To report on all files in your home directory, type
```bash
% file *
```

At login the bash shell first sources .bash_profile or .profile (if .bash_profile exists .profile will be ignored). Child shells source .bashrc. The `zsh` sources `.zprofile` and child shells source `.zshrc`. Two older shells, csh (C shell) and tcsh, read `.login` for login shells and `.cshrc` or `.tcshrc` for all other shells.
### `cut`

Note that all these file names begin with periods or "dots"; hence they are called _dotfiles_. As we have learned, dotfiles are hidden and will only be visible with `ls -a`.
The cut command extracts selected portions of a line, based on fields separated by a delimiter
```
% cut­?d delim ­?fC1,C2,C3
```

The `.bash_profile`, `.profile`, or `.login` is to set conditions which will apply to the whole session and/or to perform actions that are relevant only at login. The `.bashrc`, `.zshrc`, or `.tcshrc` file is used to set conditions and perform actions specific to the shell and to each invocation of it. The rc stands for resource; many Unix dotfiles use this convention to set resources.
Examples:
```bash
% cut -d ' ' ?f1 /etc/resolve.conf
% cat myfile | cut -c 80
```

If you wish for your login shell to source the .bashrc also, add the lines
### `sort`
This command sorts lines of a text file, based on command-­line options. The default is to sort alphabetically, based on lexigraphical ordering (in which e.g. 100 comes before 2).
```bash
if [ -f ~/.bashrc ];
then . ~/.bashrc
fi
% sort mylist.txt
```
to the `.bash_profile` script.

Warning: NEVER put commands that run graphical displays (e.g. web browsers) in your dotfiles. If you change your `.bashrc` you can force the shell to reread it by using the shell source command.
### `uniq`

```
% source ~/.bashrc
Removes duplicate lines (file must be sorted first since it only compares lines pairwise).
```bash
% uniq mylist.txt
```

## Setting the Path
A frequent pattern is to pipe the output of sort into `uniq`
```bash
% sort animals | uniq
```

When you type a command, your path (or `$PATH`) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "`command: Command not found`", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.
### `history`

For example, suppose you have installed a program called "units" into your home directory in a folder called `units174`. Units is a simple utility that can convert Imperial to metric and vice versa, from SI to cgi, and so forth. This folder contains a `bin` subdirectory in which the executable is located. To run units, you must either directly specify the units path (`~/units174/bin/units`), or you need to have the directory `~/units174/bin` in your path. You can add it to the end of your existing path (the `$PATH` represents this) by issuing the command:
The bash shell keeps an ordered list of all the commands that you have entered. Each command is given a number according to the order it was entered.
```bash
% export PATH=$PATH:$HOME/units174/bin
% history (show command history list)
```
If you are using the `bash` or `tcsh` shell, you can use the exclamation character (`!`) to recall commands easily.

If you have `units` installed you can test that this worked by trying to run units in any directory other than where units is actually located.
```bash
% !! (recall last command)
% !-3 (recall third most recent command)
% !5 (recall 5th command in list)
% !grep (recall last command starting with grep)
```
% cd; units
You can increase the size of the history buffer by typing
```
% set history=100
```
Hint: You can run multiple commands on one line by separating them with a semicolon.
To add this path permanently, add the `export` line to your `.bashrc` file after the list of other commands. Make sure that you include the `$PATH` when you reset it, or you will lose access to basic system commands!
Loading

0 comments on commit 09b120e

Please sign in to comment.