Macos Bash Prompt For Input With Default

I've spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My full .bash_profile is below, feel free to take whatever you find useful and put it to good use.
  1. Macos Bash Prompt For Input With Default Download
  • The Bash task will find the first Bash implementation on your system. Running which bash on Linux/macOS or where bash on Windows will give you an idea of which one it'll select. Bash scripts checked into the repo should be set executable ( chmod +x ).
  • Example-1: Use bash getopts with single argument. In this sample script we will take single argument as an input to our script using getopts. The script currently only supports -h as input argument which will show the usage function.
  • By default the read command will take input from stdin (standard input)and store it in a variable called $REPLY. The default delimiter for read is a newline, so it will accept input until you hit the enter key. Store Input in a Variable Read always stored your input in a variable.

A (very) quick primer on .bash_profile for Mac Users

There is a hidden file in your Mac’s user directory named .bash_profile. This file is loaded before Terminal loads your shell environment and contains all the startup configuration and preferences for your command line interface. Within it you can change your terminal prompt, change the colors of text, add aliases to functions you use all the time, and so much more.

This file is often called a ‘dot file’ because the ‘.’ at the beginning of it’s name makes it invisible in the Mac Finder. You can view all invisible files in the Terminal by typing ls -al in any directory.

How to edit your .bash_profile

Jun 22, 2017 Unix/Linux bash shell script FAQ: How do I prompt a user for input from a shell script (Bash shell script), and then read the input the user provides? Answer: I usually use the shell script read function to read input from a shell script. Here are two slightly different versions of the same shell script.

For the newly initiated, here’s how you can edit the .bash_profile on your Mac.

Step 1: Fire up Terminal.app

Step 2: Type nano .bash_profile – This command will open the .bash_profile document (or create it if it doesn’t already exist) in the easiest to use text editor in Terminal – Nano.

With

Step 3: Now you can make a simple change to the file. Paste these lines of code to change your Terminal prompt.

Step 4: Now save your changes by typing ctrl +o Hit return to save. Then exit Nano by typing ctrl+x

Step 5: Now we need to *activate your changes. Type source .bash_profile and watch your prompt change.

My .bash_profile

Macos bash prompt for input with default settingsMacos Bash Prompt For Input With Default

I have been compiling my own set of aliases and configurations for years in which time I have borrowed copiously from others who have made their aliases available online. I can claim credit for very little of what follows. I recently became aware of alias.sh, a site making sharing BASH aliases easy. Check it out.

Update: Due to popular demand, my bash profile is now available as a Gist for your forking pleasure.

Without further ado, here is my .bash_profile:

Update: If you enjoyed this post, you might also like:

Macos Bash Prompt For Input With Default Download

  • A collection of Bash scripting utilities I compiled to make custom Bash programming easier.