If you're using bash, you'd better use an here string instead of a pipe as in:
bc <<< "scale=2;$var1/$var2"
This will save you a subshell.
Then, to store the output of a command, use a command substitution:
answer=$(bc <<< "scale=2;$var1/$var2")
Edit.
If you want something even cooler than bc
, here's dc (reverse polish calculator):
answer=$(dc <<< "2k $var1 $var2/p")
As is the case with most programming, solving the problem was extremely hard. I had to study a bunch about Bash variables, and how to use export
and source
(or the POSIX dot operator, .
), and how bash loads, and what interactive -i
bash mode was, etc etc.
I found man bash
and man test
to also be useful. Here's how to do what I want to do, which is:
- Write a script to open 3 tabs.
- cd into a different folder in each tab (ie: run a unique command).
- get each tab to have a unique title by modifying its local PS1 variable
- Ensure each tab stays open after the script is run
1st, add this to the bottom of your ~/.bashrc
file:
# Function to allow a user to arbitrarily set the terminal title to anything
# Example: `set-title this is title 1`
set-title() {
# Set the PS1 title escape sequence; see "Customizing the terminal window title" here:
# https://wiki.archlinux.org/index.php/Bash/Prompt_customization#Customizing_the_terminal_window_title
TITLE="\[\e]2;$@\a\]"
PS1=${PS1_BAK}${TITLE}
}
# Back up original PS1 Prompt 1 string when ~/.bashrc is first sourced upon bash opening
if [[ -z "$PS1_BAK" ]]; then # If length of this str is zero (see `man test`)
PS1_BAK=$PS1
fi
# Set the title to a user-specified value if and only if TITLE_DEFAULT has been previously set and
# exported by the user. This can be accomplished as follows:
# export TITLE_DEFAULT="my title"
# . ~/.bashrc
# Note that sourcing the ~/.bashrc file is done automatically by bash each time you open a new bash
# terminal, so long as it is an interactive (use `bash -i` if calling bash directly) type terminal
if [[ -n "$TITLE_DEFAULT" ]]; then # If length of this is NONzero (see `man test`)
set-title "$TITLE_DEFAULT"
fi
2nd, create this script to open 3 unique tabs with 3 unique cd commands and 3 unique titles:
open_tabs.sh:
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 1'; cd ..; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 2'; cd ../..; exec bash;"
gnome-terminal --tab -- bash -ic "export TITLE_DEFAULT='title 3'; cd ../../..; exec bash;"
Now open up a terminal and run the open_tabs.sh
script:
./open_tabs.sh
Voila! It's magical! These 3 new tabs are now shown at the top of my terminal, and each has done the proper cd
command I set, and each has the proper title I set!
This will all be placed into my dotfiles project: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles.
Full and final solution: see here: Open Terminal with multiple tabs and execute application
Best Answer
When you invoke a script like
the parameter1, parameter2 etc. values from the command line are available inside the script as positional parameters
$0
,$1
, ... etcSo in your case if you want to run your script like
to install program, then inside the script you can write
See for example Handling positional parameters at http://wiki.bash-hackers.org