A command shell for Linux with very limited functionality, made in the C programming language.
This is a student project, made for the purpose of gaining more experience with the C language, as well as studying how command shells work. More specifically: how commands are processed by the shell, what kinds of decisions have had to be made and to disambiguate what is handled by the shell compared to what is handled by other programs or commands.
Let me start out by saying some of the things the shell does not have, which are usually taken for granted:
- "Up arrow" cannot be used to go to the last executed command
- "Tab" cannot be used to autofill arguments
- Has no ability to pipe multiple commands
- The shell itself does not color code anything
- Has no scripting language, and cannot run from files
Now onto what the shell provides.
-
<command> [args..]
: Calls the programcommand
with the argumentsargs
.- Searches for
command
only inPATH
, unless prefixed by./
.
- Searches for
-
help
: Prints out a list of commands provided by the shell. (WIP) -
cd [path]
: Goes to the directorypath
.- If no path is given, goes to the home directory given by the
HOME
environment variable.
- If no path is given, goes to the home directory given by the
-
var <variable> [value] [no-overwrite]
: Exports the environment variable with namevariable
.- If no value is given, the variable is unset.
- If no-overwrite is set, and the variable exists, the value will not be changed, and an error will be printed out.
-
exit
: Exits the shell.
Environment variables can be edited by using the var
keyword as mentioned above.
Passing their values can be done by prefixing $
to the variable name.
When accessing variables in this way, the characters of the variable names can contain only alphanumeric or underscores, except the first character, which cannot be a digit.
var my_var Hello
will set the variable my_var
to Hello
.
echo $my_var
will be treated as if it were echo Hello
.
Hence it will output Hello
.
There are 3 quotation marks that are recognized by the shell:
"
double quotes'
single quotes`
backticks
And they all have a few differences.
Any quotation mark that has not been closed, will be automatically closed at the end of the line.
So echo "asd
will be treated as echo "asd"
Most common escape sequences can be escaped in double quotes: \n
, \t
, \r
, \\
, \'
, \`
.
Single quotes accept all characters as raw characters, nothing can be escaped. This means that they cannot contain single quotes inside of them.
Only 2 characters can be escaped inside of backticks: \\
and \`
.
The arguments are separated by any whitespace character not in any quotation marks, some common ones are
and \t
.
As long as there are no whitespaces between any characters, they will be treated as one argument, this allows for mixing between different quotations and environment variables.
var my_var Hello
echo `in backticks`"\nin double quotes\n"'in single quotes'$my_var
In the line above, everything after echo
will be passed as one argument, as there are no spaces between the quotation marks.
And the output will be
in backticks
in double quotes
in single quotesHello