This project is about creating a simple shell. Yes, your own little bash. You will learn a lot about processes, file descriptors, redirections and more.
This project is a start into the world of SHELL development. The goal is to make a SHELL, losely based on bash.
Requirements
Your shell should:
- Display a prompt when waiting for a new command.
- Have a working history.
- Search and launch the right executable (based on the PATH variable or using a relative or an absolute path).
- Not use more than one global variable. Think about it. You will have to explain its purpose.
- Not interpret unclosed quotes or special characters which are not required by the subject such as \ (backslash) or ; (semicolon).
- Handle ’ (single quote) which should prevent the shell from interpreting the meta- characters in the quoted sequence.
- Handle " (double quote) which should prevent the shell from interpreting the meta- characters in the quoted sequence except for $ (dollar sign).
- Implement redirections:
- '<' should redirect input.
- '>' should redirect output.
- '<<' should be given a delimiter, then read the input until a line containing the delimiter is seen. However, it doesn’t have to update the history!
- '>>' should redirect output in append mode.
- Implement pipes (| character). The output of each command in the pipeline is connected to the input of the next command via a pipe.
- Handle environment variables ($ followed by a sequence of characters) which should expand to their values.
- Handle $? which should expand to the exit status of the most recently executed foreground pipeline.
- Handle ctrl-C, ctrl-D and ctrl-\ which should behave like in bash.
- In interactive mode:
- ctrl-C displays a new prompt on a new line.
- ctrl-D exits the shell.
- ctrl-\ does nothing.
- Your shell must implement the following builtins:
- echo with option -n
- cd with only a relative or absolute path
- pwd with no options
- export with no options
- unset with no options
- env with no options or arguments
- exit with no options
shell variable "" underscore info https://unix.stackexchange.com/questions/436615/when-is-an-environment-variable-of-a-bash-shell $, an underscore:
- At shell startup, set to the pathname used to invoke the shell. When you invoke a new shell, the $_ parameter in the new shell will be set to the value of the $_ parameter in the parent shell. This means that the $_ parameter in the new shell will contain the last argument of the command that invoked the new shell! TODO ? fix second functionality of $_
- is a special parameter that expands to/contains the last argument to the previous simple command that was executed. $_ is set automatically by Bash after each command is executed, regardless of whether it was successful or not. It is often used as a quick way to reference the last argument without having to retype it. If the previous command didn't have any arguments, $_ will be empty. $_ is just a regular parameter like any other, which means you can use it in the same way you would use any other parameter. For example, you can pass it as an argument to a command, or use it in a variable assignment. $_ is not an environment variable, which means it is not available to programs that are launched from within your Bash session.
dup2(old_fd, new_fd): The dup2() function duplicates the old file descriptor to the new (standard) file descriptor, which means that any subsequent write to the new_fd (standard output) will actually write to old_fd (the file). if (dup2(old_fd, new_fd) == -1) // redirect from old_fd to new_fd return (ERROR and close(new_fd))
The close() function must close the file descriptor that was returned by open().
// handle append redirection
else if (redirect->redir == APP) // check if there is append redirection { fd_file = open(redirect->file, O_WRONLY | O_APPEND | O_CREAT, 0644); // if so: open outfile and save it in fd_file if (fd_file < 0) return (minishell_error("failed to open append file")); if (dup2(fd_file, STDOUT_FILENO) == -1) // redirect stdout to fd_file return (minishell_error("Dup error stdoutput < - > write end of pipe\n")); close(fd_file); }