Basics

Posted 29 May 2016

In this series we will explore the Linux shell scripting language. Every operating system has a terminal - Windows, Linux/Unix, OSX. A terminal is textual interface to operating system. In the old days of computing (think before 90ies), computers could not display pictures or videos - all they could show was text.

Even nowadays, most servers running the internet do not have graphical interfaces, the terminal is so powerful that you can control a machine with it very nicely - there is no need for animated cat gifs :))

So let’s see how this powerful interface can be used to control machines and do awesome things.

The terminal is basically an interpreter interpreting a language called shell. This language is just like most other languages (like Java or Javascript for example), but the language itself looks rather different. “Functions” are essentially binaries (programs) on the machine and arguments are separated by spaces. The hello world example looks like the following:

echo "Hello world, this is my first shell command :-)"
Hello world, this is my first shell command :-)

This is not so exciting so far! We don’t see any context. Where is echo coming from? What is it doing? Where are we? The latter can be answered by the pwd command, which is a short name for print working directory

pwd
/

What is this / thing? We are currently at the root (or top level in other words) of the directory tree. If we do an ‘ls’ (short for list), we will see the directories in this root directory. The list will be a bit long, pre prepared :), and scroll to see all of it.

ls
bin boot dev etc home lib lib64 media mnt opt proc root run run.sh sbin srv sys tmp usr var

As we can see there are a number of directories and files on the top level. You can explore them by using the ls command and supplying folders to it. How can we have a look inside, let’s say the /bin folder?

ls /bin
bash cat chgrp chmod chown cp dash date dd df dir dmesg dnsdomainname domainname echo egrep false fgrep findmnt grep gunzip gzexe gzip hostname journalctl kill ln login loginctl ls lsblk mkdir mknod mktemp more mount mountpoint mv networkctl nisdomainname pidof ps pwd rbash readlink rm rmdir run-parts sed sh sh.distrib sleep stty su sync systemctl systemd systemd-ask-password systemd-escape systemd-inhibit systemd-machine-id-setup systemd-notify systemd-tmpfiles systemd-tty-ask-password-agent tailf tar tempfile touch true umount uname uncompress vdir wdctl which ypdomainname zcat zcmp zdiff zegrep zfgrep zforce zgrep zless zmore znew

Wow! That’s a long list. The bin folder contains some of the binaries, the programs, or commands you can run. If you look closely echo, pwd, ls are all there in the list! Using the which command we can see (surprise!) which binary will be run when we type a command name into the shell:

which ls
/bin/ls

As you can see, when we did an ls on the /bin folder, the ls command was indeed there! So meta :-D! What can you do if you want to peek inside a file? Let’s say we want to read the PERL (which is a programming language) readme on Ubuntu:

cat /usr/share/doc/perl/README.Debian
Perl Packages for Debian ======================== perl - Larry Wall's Practical Extracting and Report Language. perl-base - The Pathologically Eclectic Rubbish Lister. perl-doc - Perl documentation. perl-debug - Debug-enabled Perl interpreter. libperl5.22 - Shared Perl library. perl-modules-5.22 - Architecture independent core Perl modules. libperl-dev - Perl library: development files. To provide a minimal working perl, the ``perl-base'' package provides the /usr/bin/perl binary plus a basic set of libraries. The remainder of the application is included in the perl, perl-modules-5.22 and perl-doc packages. See the 'README.source' file in the perl source package for information on building the package. perl-suid removed ================= suidperl was removed upstream with 5.12, so the perl-suid package which used to be distributed in Debian has been removed too. Possible alternatives include using a simple setuid C wrapper to execute a perl script from a hard-coded location, or using a more general tool like sudo. Credits ------- Previous maintainers of Debian Perl packages: Ray Dassen <jdassen@WI.LeidenUniv.NL>, Carl Streeter <streeter@cae.wisc.edu>, Robert Sanders <Robert.Sanders@linux.org> and Darren Stalder <torin@daft.com>. -- Brendan O'Dea <bod@debian.org> Tue, 8 Mar 2005 19:30:38 +1100

All posts in the same series:

  1. Basics

Hi there!

crufter.com is a multi-author site for tutorials and articles. We support interactive code execution out of the browser, so your tutorial readers can learn without installing anything on their machine!

The website itself is completely open source, send a PR with improvements or your own content here.