Macos Bash Prompt For Input With Default
Bash and Z Shell. Bash, otherwise known as the Bourne Again Shell, started in 1989 to replace the older Bourne shell. It’s been the default shell in macOS starting with OS X Jaguar released in 2002. Uses: read command is used for getting user input in a Linux shell script.-p switch with read command is used for showing some helpful text on-screen. Create a shell script named input.sh and add following content.
- Macos Bash Prompt For Input With Default Download
- Macos Bash Prompt For Input With Default
- Macos Bash Prompt For Input With Default Password
From Wikipedia:
The default is PS3 – The value of this parameter is used as the prompt for the select command PS4 – The value of this parameter is expanded as with PS1 and the value is printed before each command bash displays during an execution trace. “bash (an abbreviation for “Bourne-again shell”) is the default shell for Unix-based operating systems” This is not true. Bash is the default interactive shell on most Linux distributions and macOS, yes. It is, however, not the default – or even included in the base system – on any BSD variant I’ve come across.
- A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems. Users direct the operation of the computer by entering commands as text for a command line interpreter to execute or by creating text scripts of one or more such commands.
List of shells
Shells that are more or less POSIX compliant are listed under #POSIX compliant, while shells that have a different syntax are under #Alternative shells.
POSIX compliant
These shells can all be linked from /usr/bin/sh
. When Bash, mkshAUR and zsh are invoked with the sh
name, they automatically become more POSIX compliant.
- Bash — Bash extends the Bourne shell with command-line history and completion, indexed and associative arrays, integer arithmetic, process substitution, here strings, regular expression matching and brace expansion.
- https://www.gnu.org/software/bash/ || bash
- Dash — Descendant of the NetBSD version of the Almquist SHell (ash). A fast POSIX-compliant shell that aims to be as small as possible.
- http://gondor.apana.org.au/~herbert/dash/ || dash
- KornShell (ksh) — The KornShell language is a complete, powerful, high-level programming language for writing applications, often more easily and quickly than with other high-level languages. This makes it especially suitable for prototyping. ksh has the best features of the Bourne shell and the C shell, plus many new features of its own. Thus ksh can do much to enhance your productivity and the quality of your work, both in interacting with the system, and in programming. ksh programs are easier to write, and are more concise and readable than programs written in a lower level language such as C.
- http://www.kornshell.com || See the article.
- Yash — Yet another shell, is a POSIX-compliant command line shell written in C99 (ISO/IEC 9899:1999). Yash is intended to be the most POSIX-compliant shell in the world while supporting features for daily interactive and scripting use.
- https://yash.osdn.jp || yashAUR
- Zsh — Shell designed for interactive use, although it is also a powerful scripting language. Many of the useful features of Bash, ksh, and tcsh were incorporated into Zsh; many original features were added. The introductory document details some of the unique features of Zsh.
- https://www.zsh.org/ || zsh
Alternative shells
- C shell (tcsh) — Command language interpreter usable both as an interactive login shell and a shell script command processor. It includes a command-line editor, programmable word completion, spelling correction, a history mechanism, job control and a C-like syntax.
- https://www.tcsh.org || tcsh
- Elvish — Elvish is a modern and expressive shell, that can carry internal structured values through pipelines. This feature makes possible avoiding a lot of complex text processing code. It features an expressive programming language, with features like exceptions, namespacing and anonymous functions. It also has a powerful readline which checks the syntax while typing, and syntax highlighting by default.
- https://elv.sh || elvishAUR
- fish — Smart and user-friendly command line shell. Fish performs full-color command line syntax highlighting, as well as highlighting and completion for commands and their arguments, file existence, and history. It supports complete-as-you-type for history and commands. Fish is able to parse the system's man pages in order to determine valid arguments for commands, allowing it to highlight and complete commands. Easy last-command revision can be done using
Alt+Up
. The fish daemon (fishd) facilitates synchronized history across all instances of fish, as well as universal and persistent environment variables. Additionally, fish features significantly simplified programming syntax and control flow (similar to ruby). For more information, see the tutorial.
- https://fishshell.com/ || fish
- ion — Ion is a modern system shell that features a simple, yet powerful, syntax. It is written entirely in Rust, which greatly increases the overall quality and security of the shell, eliminating the possibilities of a ShellShock-like vulnerability, and making development easier. It also offers a level of performance that exceeds that of Dash, when taking advantage of Ion's features. While it is developed alongside, and primarily for, RedoxOS, it is a fully capable on other *nix platforms. For more details lookup its manual.
- https://gitlab.redox-os.org/redox-os/ion/ || ion-gitAUR
- Nash — Nash is a system shell, inspired by plan9 rc, that makes it easy to create reliable and safe scripts taking advantages of operating systems namespaces (on linux and plan9) in an idiomatic way.
- https://github.com/NeowayLabs/nash || nash-gitAUR
- Oh — Unix shell written in Go. It is similar in spirit but different in detail from other Unix shells. Oh extends the shell's programming language features without sacrificing the shell's interactive features.
- https://github.com/michaelmacinnis/oh || oh-gitAUR
- PowerShell — PowerShell is an object-oriented programming language and interactive command line shell, originally written for and exclusive to Windows. Later on, it was open sourced and ported to macOS and Linux.
- https://github.com/PowerShell/PowerShell || powershellAUR
- rc — Command interpreter for Plan 9 that provides similar facilities to UNIX’s Bourne shell, with some small additions and less idiosyncratic syntax.
- http://doc.cat-v.org/plan_9/4th_edition/papers/rc || 9base
- xonsh — Python-powered shell with additional shell primitives that you are used to from Bash and IPython.
- https://xon.sh/ || xonsh
Changing your default shell
After installing one of the above shells, you can execute that shell inside of your current shell, by just running its executable. If you want to be served that shell when you login however, you will need to change your default shell.
To list all installed shells, run:
And to set one as default for your user do:
If you are using systemd-homed, run:
where full-path-to-shell is the full path as given by chsh -l
.
If you now log out and log in again, you will be greeted by the other shell.
/etc/shells
as reference. If a recently installed shell is not present on the list, it can be manually added to this file.Login shell
A login shell is an invocation mode, in which the shell reads files intended for one-time initialization, such as system-wide /etc/profile
or the user's ~/.profile
or other shell-specific file(s). These files set up the initial environment, which is inherited by all other processes started from the shell (including other non-login shells or graphical programs). Hence, they are read only once at the beginning of a session, which is, for example, when the user logs in to the console or via SSH, changes the user with sudo or su using the --login
parameter, or when the user manually invokes a login shell (e.g. by bash --login
).
See #Configuration files and the links therein for an overview of the various initialization files. For more information about login shell, see also Difference between Login Shell and Non-Login Shell? and Why a 'login' shell over a 'non-login' shell? on Stackexchange.
Configuration files
To autostart programs in console or upon login, you can use shell startup files/directories. Read the documentation for your shell, or its ArchWiki article, e.g. Bash#Configuration files or Zsh#Startup/Shutdown files.
See also Wikipedia:Unix shell#Configuration files for a comparison of various config files of various shells.
/etc/profile
Upon login, all Bourne-compatible shells source /etc/profile
, which in turn sources any readable *.sh
files in /etc/profile.d/
: these scripts do not require an interpreter directive, nor do they need to be executable. They are used to set up an environment and define application-specific settings.
Standardisation
It is possible to make (some) shells configuration files follow the same naming convention, as well as supporting some common configuration between the shells.
See the article about this and the related repository. See also xsh.
Input and output
See also GregsWiki and I/O Redirection.
- Redirections truncate files before commands are executed: will therefore not work as expected. While some commands (sed for example) provide an option to edit files in-place, many do not. In such cases you can use the sponge(1) command from the moreutils package.
- Because cat is not built into the shell, on many occasions you may find it more convenient to use a redirection, for example in scripts, or if you care a lot about performance. In fact
< file
does the same ascat file
. - POSIX-compliant shells support Here Documents:
- Shell pipelines operate on stdout by default. To operate on stderr(3) you can redirect stderr to stdout with
command 2>&1 | othercommand
or, for Bash 4,command |& othercommand
. - Remember that many GNU core utilities accept files as arguments, so for example
grep pattern < file
is replaceable withgrep patternfile
.
See also
- Evolution of shells in Linux on the IBM developerWorks
- terminal.sexy — Terminal Color Scheme Designer
- Hyperpolyglot — Side-by-side comparison of shell syntaxes
- UNIX Power Tools — General command-line tool usage
- commandlinefu.com — Command-line snippets sharing
This article is part two in a series about using Linux skills when working in a mac operating system. Check out the other articles in the series:
As you now know from my introductory article, macOS is a flavor of UNIX, similar to Linux. But unlike Linux, macOS does not support virtual terminals by default. Instead, you can use the Terminal app (/Applications/Utilities/Terminal) to obtain a command line terminal and BASH shell.
Alternatively, you can type >console as the username at the macOS login prompt (no password needed) to obtain a login prompt that allows you to log directly into a command line terminal and obtain a BASH shell.
After you obtain a BASH shell, you can run many of the same commands you’ve become accustomed to on Linux. It’s also important to note that the same syntax rules that you used on Linux apply to BASH on macOS:
- All commands are case sensitive.
- Regular options (-h) and POSIX options (--help) are listed for most commands.
- The manual pages (man who) and the info pages (info who) list most command documentation. The help command displays BASH function help (help echo).
- Commands may be chained using semicolons (date; who).
- The Standard Output of one command may be sent to the Standard Input of another command using a pipe ( | ) symbol (ps aux | less).
- The Standard Output and Standard Error of a command may be redirected to a file (ls –l /Users > usersfile 2> errorfile).
- Single quotes protect all special shell characters, double quotes protect most shell characters (variables are one exception), backquotes perform command substitution (echo The date is `date`) and backslash protects the next character (echo You owe $4.50).
- The ~ metacharacter represents the user’s home directories.
- Tab completes commands and file paths.
- Ctrl+C cancels a command by sending a SIGINT kill signal to the foreground process.
- Wildcard metacharacters may be used with commands, and regular expressions may be used within text utilities.
- BASH control structures and functions may be used to create BASH shell scripts.
NOTE: If you wish to perform system administration as the root user, you must first enable the root user using the Directory Utility app (in /System/Library/CoreServices/Applications). Once you open this app, simply select Change Root Password from the Edit menu.
Macos Bash Prompt For Input With Default Download
Common macOS Commands
Below are some common macOS commands, organized by general function. Bookmark this page so you can refer back to it often. Although most options to these commands are identical between Linux and macOS, be sure to view the manual page for commands that you will use in the future.
System Documentation Commands in macOS
- man Displays/searches the manual page database
- apropos Searches the manual page database by keyword
- info Displays/searches the info page database
- help Displays help on BASH functions
File Management Commands in macOS
- pwd Displays the current directory
- cd Changes the current directory
- ls Lists files
- file Displays file type
- locate Finds files using a predefined database
- which Searches the PATH variable for files
- find Finds files on the filesystem based on certain criteria
- cp Copies files and directories
- mv Moves/renames files and directories
- rm Removes files and directories
- rmdir Removes empty directories
- mkdir Creates empty directories
- ln Creates hard links; the –s option is used to create symbolic links
- chown Changes ownership for files and directories
- chgrp Changes group ownership for files and directories
- chmod Changes the permissions (mode) on files and directories
- umask Changes the UMASK variable used to determine default permissions
Macos Bash Prompt For Input With Default
Text Tools in macOS
- cat Views (concatenates) the contents of text files to the terminal screen
- more Views the contents of text files page by page
- less Views the contents of text files page by page with advanced text features
- head Displays the beginning of a text file
- tail Displays the end of a text file
- sort Sorts lines in text files
- wc Counts the number of lines, words and characters in a text file
- grep Searches text files for regular expressions
- egrep Searches text files for regular expressions (including extended ones)
- sed Edits text files using search-and-replace functions
- awk Edits and formats text using predefined functions
- ·vi (vim) The vi text editor (common to all UNIX systems)
- emacs The GNU Emacs text editor
- ·nano A small easy-to-use text editor based on the UNIX pico editor
Filesystem Administration in macOS
- mount Mounts a filesystem to a mount point directory – alternatively, you can use the mount_filesystem commands (where filesystem is the name of an appropriate filesystem) – see man mount for details.
- umount Unmounts a filesystem from a mount point directory
- pdisk May be used to create Apple disk partitions
- newfs_type Creates a new filesystem on a device (type = apfs, hfs, udf, exfat, msdos)
- fuser Determines the users accessing a certain file, directory or terminal
- df Displays disk free space by filesystem
- du Displays disk free space by directory
- quota Displays quotas for a certain user
- edquota Edits user quotas
- repquota Displays a report on quotas by user
- quotacheck Updates quota limits on the filesystem
Disk quotas are configured differently in macOS. In Linux, you enable quotas using the appropriate option in /etc/fstab. However, /etc/fstab is not available in macOS. Instead, you must create two files in the root of the filesystem that you wish to enable quotas on (.quota.ops.user and .quota.ops.group).
Shutdown and System State in macOS
- shutdown Shuts down or reboots the system at a specified time
- halt Shuts down the system immediately
- reboot Reboots the system immediately
Compression, Backup and Software in macOS
- compress Compresses files using a Lempel-Ziv algorithm
- gzip Compresses files using a standard Lempel-Ziv algorithm
- bzip2 Compresses files using a block-sorting algorithm
- tar Used to create small tar archives and tarballs
- cpio Used to create full filesystem backups using a variety of options
- make Manages software compiling using gcc and Makefile settings
- gcc The GNU C compiler used to compile software
BASH Management in macOS
As in Linux, macOS stores its variables in environment files. The /etc/profile and /etc/bashrc files are used by default on the system. Each user can also create their own ~/.bash_profile and ~/.bashrc files. In addition, ~/.bash_logout may be used to perform tasks at shell exit. Here are more BASH management commands for macOS.
- set Displays all variables in your shell
- env Displays exported variables in your shell
- alias Creates special alias variables
- unalias Removes special alias variables
- export Creates and exports variables
- ulimit Sets BASH limits for users (e.g., maximum number of user processes)
Process Management in macOS
- ps Displays system and user processes (supports BSD-style options only)
- top Displays top processes and system statistics
- kill Sends kill signals to processes by process identification number (PID)
- killall Sends kill signals to processes by name
- jobs Displays background processes
- fg Moves a background process to the foreground
- bg Moves a foreground process to the background
- nice Changes the priority of a process as it is started
- renice Changes the priority of a running process
- at Schedules commands to run at a later time
- atq Views at jobs
- atrm Removes an at job
- crontab Edits user cron tables (used to repetitively schedule commands)
User and Group Administration in macOS
User and group administration differs slightly in macOS from Linux. There are no useradd or userdel commands. Instead, you must use the System Preferences utility to create user accounts properly. User information is stored in a directory database under the /var/db directory. The /etc/shadow file does not exist and the /etc/passwd and /etc/group files exist only to provide information to apps.
- whoami Displays the current user name
- who am i Displays your username and computer information
- groups Displays the current user’s group memberships
- id Prints the User ID (UID) and Group IDs (GIDs) for the current user
- chfn Changes the user description used by the finger command
- finger Displays user description information
- chsh Changes the shell for a user account
- passwd Changes the password for the current user (the root user may change other user's passwords by specifying the username as an argument)
- who Displays who is on the system
- w Displays who is on the system and what they are doing
Macos Bash Prompt For Input With Default Password
Common Unix Printing System (CUPS) Printing in macOS
- lp Prints a file (lpr is supported for BSD compatibility)
- lpstat Views print jobs and printer status (lpq and lpc are supported for BSD
- compatibility)
- cancel Removes a print job from the print queue (lprm is supported for BSD
- compatibility)
- cupsaccept Allows jobs to enter a print queue
- cupsreject Prevents jobs from entering a print queue
- cupsenable Allows jobs to be sent to the printer from the print queue
- cupsdisable Prevents jobs from leaving the print queue
Network- and Security-Related Commands in macOS
Network devices have different names in macOS. The first ethernet adapter is called en0 (typically wired ethernet), and the second is called en1 (typically wireless ethernet). Ensure that you use System Preferences to change any network parameters, as they are not stored in text files as they are on Linux systems. For example, the /etc/resolv.conf file still exists in macOS and lists DNS servers configured in System Preferences, but it is not used by the system – it is merely there in case an app queries it. The only network-related file that is used actively and may be edited is /etc/hosts (for local host name resolution). Here are more network- and security-related commands in macOS.
- ifconfig Displays and configures TCP/IP network interfaces
- ping Tests connectivity between hosts
- whois Queries domain name registration information
- arp Views and manages the address resolution protocol (ARP) cache
- netstat Views TCP/IP network statistics and the routing table
- route Manages the TCP/IP routing table
- traceroute Traces an IP packet across routers
- hostname Sets the system host name
- host Resolves host names to IP addresses and vice versa
- nslookup Resolves host names to IP addresses and vice versa
- dig Resolves host names to IP addresses and vice versa
- su Switches your user account to another account
- sudo Performs tasks as another user via entries in the /etc/sudoers file
- last Displays a detailed list of previous user logins
- tcpdump Captures packets on a network interface
System and Miscellaneous Commands in macOS
- date Displays the current date and time
- exit Exits out of the shell (logout)
- echo argument Displays the argument to the terminal screen
- clear Clears the screen
- uname option Displays system information specified by the option; –a specifies all information
- uptime Lists system statistics and uptime
- cal Lists the calendar for the current month
- banner Prints an ASCII banner
- iostat Displays input/output (I/O) statistics for the system
Now you’ve got the macOS basics down, and you know the common commands. In the next installment, I’ll be discussing apps. Stay tuned!
Round out your IT skillset with CompTIA Linux+.