What happens when you type ls *.c and hit Enter in your shell?

Christian Rojas
5 min readAug 25, 2021

--

As we know ls command lists files and directories.

ls command can be followed by options and arguments to supplement the functionality of the command.

* — is a wildcard character that can be used as a substitute for an array of characters in a search.

* — can be used with strings or characters/symbols to refine or modify parameters of a search.

ls *(string) will result in finding any and all files with the string pattern at the ending of the filename. Vice-versa if we do it ls (string)* it will search for the string pattern at the beginning of the filename.

ls *.c will try to list all files with the .c extension, if no files are found the command line output prompt will be something like this “ls: cannot access *.c: No such file or directory”

ls

ls *.c

A shell does three main things:

  • Initialize: In this step, a typical shell would read and execute its configuration files. These change aspects of the shell’s behavior.
  • Interpret: Next, the shell reads commands from the standard input, that is the line prompt, and executes them.
  • Terminate: After its commands are executed, the shell executes any shutdown commands, frees up any memory, and terminates. In this step, a typical shell would read and execute its configuration files. These change aspects of the shell’s behavior.

fork()

creates a child process that differs from the parent process only in its PID and PPID, and in the fact that resource utilizations are set to 0. File locks and pending signals are not inherited. On success, the PID of the child process is returned in the parent’s thread of execution, and a 0 is returned in the child’s thread of execution. On failure, a -1 will be returned in the parent’s context, no child process will be created, and errno will be set appropriately.

execve()

executes the program pointed to by filename. filename must be either a binary executable, or a script starting with a line of the form “#! interpreter [arg]”. In the latter case, the interpreter must be a valid pathname for an executable which is not itself a script, which will be invoked as interpreter [arg] filename. argv is an array of argument strings passed to the new program. envp is an array of strings, conventionally of the form key=value, which are passed as environment to the new program. Both argv and envp must be terminated by a null pointer. The argument vector and environment can be accessed by the called program’s main function, when it is defined as int main(int argc, char *argv[], char *envp[]).

$ wait

wait is a built-in command of Linux that waits for completing any running process. wait command is used with a particular process id or job id. When multiple processes are running in the shell then only the process id of the last command will be known by the current shell. If wait command is executed this time, then it will be applied for the last command. If no process id or job id is given with wait command then it will wait for all current child processes to complete and returns exit status.

Aliases

An alias is a shorthand shell notation that allows you to customize and abbreviate commands. Aliases are available in all shells. A common syntax to define an alias on command line is as follows:

$ alias name=command_string

If the first word on the command line is an alias, the shell replaces that word with the text of the alias. The shell maintains a list of aliases that it searches when a command is entered. The following rules apply while creating an alias. There can be no space on either side of the equal sign. The command string must be quoted if it includes any options, metacharacters, or spaces. Each command in a single alias must be separated with a semicolon.

Prompt

The shell prompt is the visual aspect of the shell between running programs, the epart that shows it is waiting for you to give it a command.

Environmment

A shell maintains an environment that includes a set of variables defined by the login program, the system initialization file, and the user initialization files. In addition, some variables are defined by default. A shell can have two types of variables:

Environment variables: Variables that are exported to all processes spawned by the shell. Their settings can be seen with the env command. A subset of environment variables, such as PATH, affects the behavior of the shell itself.

Shell (local) variables: Variables that affect only the current shell. In the C shell, a set of these shell variables have a special relationship to a corresponding set of environment variables. These shell variables are user, term, home, and path. The value of the environment variable counterpart is initially used to set the shell variable.

PATH

PATH is an environmental variable in Linux and other Unix-like operating systems that tells the shell which directories to search for executable files (ready-to-run programs) in response to commands issued by a user. It increases both the convenience and the safety of such operating systems and is widely considered to be the single most important environmental variable.

--

--