Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 4 years ago.
Improve this question
I have two laptops--one for work, one for personal stuff. My .bash_profile on each has a lot of the same functions and aliases. However, there is some stuff on my work laptop's .bash_profile that doesn't exist on my personal laptop's.
I'm wondering if there is a way I can make a "shared" .bash_profile or something, and extend functionality to it so it's easier to share my bash stuff between laptops.
I'm using a Mac if that matters; bash --version == 4.4.19
Sure it's possible, and not that uncommon to break up a .bash_profile into modules using the source command.
From your question it sounds like your .bash_profiles are almost identical, but the only difference is that you have work specific stuff on your work laptop. One way you could break it up would be to move your work aliases, functions, exports, etc. to their own file. We'll call it ~/.work_profile
Then in your ~/.bash_profile add the following.
[[ -f ~/.work_profile ]] && source ~/.work_profile
An explanation: [[ -f ~/.work_profile ]] tests that the regular file ~/.work_profile exists and will return true if it does.
&& source ~/.work_profile is a logical and. If the previous command returns true, then it will run this part and source your ~/.work_profile.
Solution 1:
Put common stuff in the shared .bash_profile
source ~/.bashrc.local at the end of .bash_profile
Each laptop has its own .bashrc.local.
Solution 2: Use one single .bash_profile on both laptops.
... common stuff ...
if [[ -f /a/file/only/on/work/laptop ]]; then
... now on work laptop ...
else
... now on home laptop ...
fi
I have 20+ Bash rc files so my real solution is a bit more complicated:
There's only one line in ~/.bashrc_profile:
source ~/.bashrc
All systems share the same ~/.bashrc in which the logic is like this:
# Each system has its own `.bashrc.pre' which defines the
# var `$RC_DIR' for top dir of all rc files (Bash, Vim, ...).
#
# I need to define my own `$RC_DIR' because my coworkers share
# some servers (using the same accounts) at work.
source ~/.bashrc.pre
# `rcfiles' is system specific which lists rc files to be
# loaded on that system. I maintain a `rcfiles.template'.
for file in $(< $RC_DIR/bash/rcfiles); do
source $file
done
# `.bashrc.post' is system specific
source ~/.bashrc.post
You can either use the profile file in /etc if you have root, or use source filename to include other (common) file.
You may also want to have a look at this question explaining the different loadable files.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 1 year ago.
Improve this question
I am completely new to Mac OS and I am trying to figure out how to display in terminal all flags for echo command. When I do man echo it only shows me one flag which is -n and I am pretty sure there are many more.
I've googled it but found nothing.
Thank you beforehand!
For shell builtins such as echo, the man page does not offer an accurate description, since it describes the binary (usually found in /bin), not the builtin.
For help on the built-in commands in Bash, use
help echo
For instance, on my version of macOS this shows me:
echo: echo [-neE] [arg ...]
Write arguments to the standard output.
Display the ARGs, separated by a single space character and followed by a
newline, on the standard output.
Options:
-n do not append a newline
-e enable interpretation of the following backslash escapes
-E explicitly suppress interpretation of backslash escapes
…
In zsh, the closest equivalent is man zshbuiltins, which shows the documentation for all built-in commands. To find a given command in it, type / echo and hit Return. Note that there are multiple spaces between / and echo! This isn’t necessary, but it often filters out false hits.
echo is a function of your shell and it behaves differently if you have a different shell. zsh or more 'advanced' shells may support extra args, but i've for one not heard about other args to echo than -n (which removes the trailing newlines).
macos is closer to freebsd than linux and it comes bundles with bash (Bourne again Shell), but in normal scenarios you should only assume that sh or ash (in Alpine) is available. E.g. while you may be able to use extra flags for echo with a different shell, you shouldn't expect that shell to be available or default on a target system.
I think a better way to find out what you want is to google what you want echo to do and likely there'll be lots of ways described how to do that outside echo (printf, cat, tee or similar).
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
I am newbie in Operation System, and trying to catch up by exploring my new computer.
This morning, I was just exploring the bin file, and I saw a weird folder called "[". What is this?
I did not create or install anything in the "/bin" folder yet. So it was not created by me. What is the "[" binary file in my "/bin" folder for?
[ is the test program. As the man page suggests, it's used like
[ condition ]
and evaluates to either a zero (for true) or a non-zero value (for false). In the case of zsh on macOS, the man page says that false is represented by 1. Note that the whitespace after the square bracket is important since that whitespace indicates to the shell that you want to run the [ program.
Despite the presence of the program, that executable is probably not used often. It appears that you're using zsh, which is the default shell on macOS. In zsh, [ is a built-in which takes precedence over /bin/[. This can be verified by
% which -a [
[: shell built-in command
/bin/[
The man page for the built-in can be found by man zshbuiltins.
If you want to explicitly use the program in /bin, then your command should look like
/bin/[ condition ]
Example
Supposed you're running a shell script where you want to check a condition. For example, you want to check whether a file exists. [ is frequently used for this purpose. Here is an example
if [ -f myFile.txt ]; then
date > myFile.txt
else
echo "File was created on $(cat myFile.txt)."
fi
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 3 years ago.
Improve this question
If I need to change to some directory several levels up, I usually do this in bash:
cd ../../../../some/other/folder
Since it is quite annoying to type all those periods and slashes, I was wondering if there is some shorter way to type it; like for example:
cd ..4/some/other/folder
I have not been able to find it so far from for example cd --help.
There's no standard way.
You can declare a function that takes a number of parent directories as the first argument, and the relative path as the second one:
cdu () {
local n=$1
local p=""
while ((n--)) ; do
p+=../
done
cd "$p/$2"
}
You can then shorten cd ../../../bin to cdu 3 bin
What I use is
alias ..='cd ..'
alias ...='cd ../..'
To get 6 levels up, I just type ... + Enter three times.
according to cd man page, the immediate answer is "no".
if it helps, you may add the following to your .bashrc:
export prev1=".."
export prev2="../.."
export prev3="../../.."
export prev4="../../../.."
and so on.
example:
export prev4="../../../.."
mkdir -p /1/2/3/4/5
cd /1/2/3/4/5
pwd => result is /1/2/3/4/5
cd $prev4
pwd => result is /1
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 7 years ago.
Improve this question
I have a git repo in a folder that is inside multiple other folders. When I want to use the command line for git, I have to do cd /Desktop/.../.../.../.../repo_folder to do git commands. Is their any way that I can set a shortcut to a folder or get to a folder faster because having to type in a 70 character long path is not ideal
Thanks in advanced!
You can use the cdable_vars option of bash that allows you to call cd with a variable name. If the argument passed to cd is not a directory, then it is assumed to be a variable name and the value of the variable is used as the destination directory.
Example of use: if you put this in your ~/.bashrc:
alias show='cat ~/.dirs'
save () {
here=`pwd`
if (( $# == 0 )); then
name=`basename $here`
elif (( $# > 1 )); then
echo "usage: save [<name>]"
return -1
else
name=$1
fi
sed -i -e "/^$name=/d" ~/.dirs
echo "$name=\"$here\"" >> ~/.dirs
source ~/.dirs
}
source ~/.dirs
shopt -s cdable_vars
Then, when your current directory is one that you want to remember, just type:
save my_dir
and the next time you want to go there, just type:
cd my_dir
As long there is no my_dir directory where you type it, it will bring you where you want. The save argument is optional. If you do not provide it the defined short hand will be the base name of the current directory:
cd /Desktop/../../../../repo_folder
save
will define repo_folder as the short hand for this directory.
The ~/.dirs file contains your variable definitions for your favourite directories. You can edit it by hand, if you wish. These definitions are evaluated every time you launch a new bash shell. Beware they may overwrite others that you also need. If it is a problem, I advice you to chose unique short hands (my_dir_repo_folder instead of repo_folder). And remember the second pitfall, when you type:
cd foo
you can go either to the local sub-directory foo if there is one or to the directory for which you defined the foo short hand. And there is a third one: if you redefine a short hand, the previous one is overwritten. So, this trick is handy but somehow dangerous because when you cd you do not know any more if you are really where you want. Customizing your prompt to show the current path may be a good idea.
The show alias is just a way to list all currently defined short hands.
Just set "start in" param in your bash shortcut properties and every time you run bash it will open your repo folder :)
Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have a program, called carmel, which I can run from the command line via:
carmel -h
or whichever suffix I chose. When loading a file, I can say:
carmel fsa1.fst where fsa1.fst is located in my heme folder, /Users/adam/.
I would prefer to have the default file location be, e.g., /Users/adam/carmel/files, and would prefer to not type that in every time. Is there a way to let UNIX know, when I type carmel to then look in that location?
There is no standard Unix shortcut for this behaviour. Some applications will check an environment variable to see where their files are. but looking at carmel/src/carmel.cc on GitHub, I'd say you'd have to write a wrapper script. Like this:
#!/usr/bin/env bash
# Save as ${HOME}/bin/carmel and ensure ${HOME}/bin is before
# ${carmel_bin_dir} in your ${PATH}. Also ensure this script
# has the executable bit set.
carmel_bin_dir=/usr/local/bin # TODO change this?
working_directory=${CARMEL_HOME-${HOME}/carmel/files}
if [[ ! -d "${working_directory}" ]]; then
echo "${working_directory} does not exist. Creating."
mkdir -p "${working_directory}" || echo "Failed to create ${working_directory}"
fi
pushd "${working_directory}"
echo "Launching ${carmel_bin_dir}/carmel ${#} from $(pwd)..."
${carmel_bin_dir}/carmel ${#}
popd
Alternatively, since the source is freely available, you could add some code to read ${CARMEL_HOME} (or similar) and submit this as a pull request.
Good luck!