The command#

Commands are our tool to tell the computer what to do. Most commands have options and arguments. Arguments are often essential for a command to operate properly; they are the pieces of information required by a command, such as a file name. Options are, of course, optional, and offer ways to modify the way the command works.

../../../_images/command_structure_with_examples.PNG

For instance, echo will take any text you give it as an argument and then send it back to you as output:

# My first command
echo 'Hello World!'

If you use the option -n, then it will not add a ‘new line’ to the end of the output:

# My second command
echo -n 'Hello World!'

Some commands end up with very complex structures, because they can have many options and arguments. In general, options will be of the format -a where a is a single letter or --word where word is a string (a series of letters, in computer terms).

Note: the command line is case-sensitive! So it does matter if you write -a or -A.

Getting help#

The man command will show a manual for most basic commands, providing the correct syntax to use it and the various options available.

# Read the manual
man ls

Other programs have different ways to provide help on how to use them. An online tutorial is best, or a comprehensive manual, but sometimes you only have the command line to help you.

# Help please!
python3 -h
python3 --help

Useful command line tricks#

  • You can use the up and down arrow keys to navigate through previously used commands (known as your history) and repeat or modify them.

  • Windows: To copy text from the terminal you will have to highlight it and right-click to use the in-browser menu and copy. Similarly you have to use the in-browser menu to paste into the terminal. The reason for this is that Ctrl + c and Ctrl + v have effects inside the terminal.

  • Mac: You can fortunately use Cmd + c and Cmd + v to copy and paste as normal. You can use Ctrl and various keys for in-terminal commands.

  • When typing a command or file name, you can press the ‘tab’ key to auto complete what you are typing. If there are multiple commands or files with similar names, auto completion will fill in as far as the first ambiguous character before you have to give it some more input. This method makes it much less likely that you make a spelling error. Also, if you double press the ‘tab’ key all the available options to complete will be shown.

  • Pressing Ctrl + c will send an interrupt signal that cancels the currently running command and brings you back to the command line.

  • Pressing Ctrl + r will allow you to search through your command history.

  • Pressing Ctrl + l will clear the screen.

  • See previous commands by typing history and pressing enter.

  • Double-click to select a word, triple-click to select a line

  • Using a # character allows you to make comments that have no effect when run.

Exercise 0.1

  • Connect to Cousteau

First, you have to open the command line interface on you computer For Windows: Type “command” into the taskbar menu For Mac: Click onto the launchpad icon in the dock and type “Terminal” into the search field

Second, connect to Cousteau with the ssh command. You need your user ID and your nethz password in order to connect to the serve. The command to connect to the Cousteau is:

ssh <your ETH-ID>@cousteau.ethz.ch
  • Try to echo “My first command”

#echoing "My first command"
echo 'My first command'
  • Use the arrow key to execute the same command again

 # Press the up arrow once and the last command appears
echo 'My first command'
  • Try typing e then pressing tab twice, what do you see?

# You see all the possible commands that start with "e" when you press tab twice after entering “e”
e2freefrag             edquota                era_check              eu-readelf
e2fsck                 efibootdump            era_dump               eu-size
e2image                efibootmgr             era_invalidate         eu-stack
e2label                efikeygen              era_restore            eu-strings
e2mmpstatus            efisiglist             esac                   eu-strip
e2undo                 efivar                 escputil               eutp
e4crypt                egrep                  espdiff                eu-unstrip
e4defrag               eject                  espeak-ng              eval
eapol_test             elfedit                ether-wake             evince
easy_install-2         elif                   ethtool                evince-previewer
easy_install-2.7       else                   eu-addr2line           evince-thumbnailer
easy_install-3         enable                 eu-ar                  evmctl
easy_install-3.6       encguess               eu-elfclassify         ex
ebtables               enchant-2              eu-elfcmp              exec
ebtables-restore       enchant-lsmod-2        eu-elfcompress         exempi
ebtables-save          enscript               eu-elflint             exit
echo                   env                    eu-findtextrel         exiv2
ed                     envsubst               eu-make-debug-archive  expand
edgepaint              eog                    eu-nm                  export
edid-decode            eps2eps                eu-objdump             exportfs
editdiff               eqn                    eu-ranlib              expr
  • Try adding c to make ec and pressing tab again. What happens?

# The command autocompletes after adding the “c” to the “e”
echo
  • Try to copy/paste your echo command “echo ‘My first command’”

# Note that ctrl + c to copy does not work in windows terminal or mobaxterm - instead highlighting text automatically copies it
# In mobaxterm ctrl + v to paste also does not work - instead right-click to paste
echo 'My first command'
  • Try to clear the screen, can you still paste your echo command?

# To clear the screen use ctrl + l or the command 'clear' and you can still paste the command
echo 'My first command'
  • Try to echo ‘My first command ‘once with the -n option and once with the -N option. What do you notice?

# echo -n does not add a new line to the output
echo -n 'My first command'
My first command[]$

# The -N option does not exist therefore “echo” will ill interpret '-N' as characters to display
echo -N 'My first command'
-N My first command
  • Try to look at the manuel of the echo command.

# man gives you the manual of the function
man echo