Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

.bashrc backup

Hi


I want to do a periodic backup of my .bashrc file in ML. Can anyone suggest any elegent way to do this? I tried using cp command to write .bashrc file content to a txt file. It worked fine. Then I put the same command in my .bashrc file so that when I login the backing up executed automatically. It does, however, I get to see the prompt which asks me whether I want to replace to orgininal file. How can I set yes for this action, so that I don't see prompt each time I start a Terminal session?


Thanks!

Tissot11

MacBook Pro with Retina display, OS X Mountain Lion, 2.6 GHz, 8 GB RAM

Posted on Jan 28, 2013 10:05 AM

Reply
27 replies

Feb 6, 2013 5:17 AM in response to rccharles

Sorry for delay.


Here are results for the different commands that you suggested to try.

-------------------------------------------------------------------------------- ----

ls -ale@ .bashrc

-rwxr-xr-x@ 1 Naveen staff 3.3K Feb 1 10:28 .bashrc

com.apple.FinderInfo 32B

com.apple.TextEncoding 15B

com.apple.quarantine 23B

-------------------------------------------------------------------------------- -

ls -ale@ Documents/Tips\ and\ Tricks/Misc./bashrc_backup.txt

-rwxr-xr-x@ 1 Naveen staff 3.3K Feb 3 21:09 Documents/Tips and Tricks/Misc./bashrc_backup.txt

com.apple.FinderInfo 32B

com.apple.TextEncoding 15B

com.apple.quarantine 23B

-------------------------------------------------------------------------------- -------

ls -aled@ Documents/Tips\ and\ Tricks/Misc.

drwxr-xr-x 5 Naveen staff 170B Feb 1 10:30 Documents/Tips and Tricks/Misc.

-------------------------------------------------------------------------------- -----------------------------------------

whereis cp

/bin/cp

-------------------------------------------------------------------------------- ---------------------------

cp -fv .bashrc bashrc_backup.txt

.bashrc -> bashrc_backup.txt

--------------------------------------------------------------------

echo ''copy to folder''

copy to folder

-----------------------------------------------------------------

cp -fv .bashrc Documents/Tips\ and\ Tricks/Misc./bashrc_backup.txt

overwrite Documents/Tips and Tricks/Misc./bashrc_backup.txt? (y/n [n]) y

.bashrc -> Documents/Tips and Tricks/Misc./bashrc_backup.txt

-------------------------------------------------------------------------------- ------------------




What would you suggest for this behviour?

Feb 6, 2013 6:27 AM in response to Tissot11

Based on the permissions shown, the cp -f should have replaced the file without asking any questions.


Instead of "whereis cp", what is


type cp


which will tell us if cp is an alias that maybe includes hidden cp option switches. We would normally expect to see


type cp
cp is /bin/cp




Another approach might be to blindly remove the old backup copy, then make a new backup


/bin/rm -f /path/to/your/backup.bashrc
/bin/cp -fp $HOME/.bashrc /path/to/your/backup.bashrc


Yet another approach is to create dated copies, but that can also lead to lots of copies, especially since .bashrc is run every time a bash subshell is started :-)


/bin/cp -fp $HOME/.bashrc /path/to/your/backup.bashrc.$(date +%Y%m%d%H%M)


The addition of the -p is to preserve the original file's attributes (permissions, timestamps, etc...).

Feb 6, 2013 6:37 AM in response to BobHarris

Indeed cp is alised to cp -i on my Mac by defaults that's why it's asking to replace the file everytime. I tried to type cp command and it gave


type cp

cp is aliased to `cp -i'


In man page on cp it says that in legeacy mode -f option would override the -i option. But this is not appears to be my case. How can I set property for this particular copying so that it doesn't ask me everytime?

Feb 6, 2013 7:18 AM in response to Tissot11

First thank rrcharles for asking the right questions. He got to the heart of the matter.


/bin/cp


as in


/bin/cp -fp $HOME/.bashrc /path/to/your/backup.bashrc


Or put the copy in .bash_profile


One of the reasons for .bash_profile vs .bashrc is scope.


.bash_profile gets invoked once during a new session, and it an idea place to put actions that only need to be done once, such as environment variable setups, copying .bashrc to a backup, checking the state of your environment (if there is anything you need to check), etc...


.bashrc is invoked every time you start a new shell (this includes sub-shells), so it is idea for things that are not inherited, such as aliases, your command prompt if you need it to be dynamic based on where and under what conditions the sub-shell is invoked, establishing in-line shell functions that behave like shell scripts but are executed out of the shell's memory, etc...


So if you did your backup in .bash_profile, and at the end of .bash_profile sourced .bashrc, then your cp alias would not occur until after your cp was completed.


But if your shell initializations are simple, and you rarely create subshells, then having a single .bashrc is fine. Just use /bin/cp for the copy and you should be fine.


Message was edited by: BobHarris

Feb 6, 2013 11:57 AM in response to BobHarris

I thought the command options overrid the alias options?


--------------------------



I have this function in one of my profiles. Types out all the info that I could think of.


# Define a new command called settings to list various options.

function settings ()

{

( echo "---------- env"; \

env; \

echo "--------- set"; \

set; \

echo "--------- export"; \

export; \

echo "--------- export -f"; \

export -f; \

echo "--------- alias"; \

alias; \

echo "--------- set -o"; \

set -o; \

echo "--------- shopt"; \

shopt; \

echo "--------- enable -a"; \

enable -a; ) | less


}

Feb 6, 2013 12:08 PM in response to rccharles

I thought the command options overrid the alias options?


man bash

"Aliases allow a string to be substituted for a word when it is used as the first word of a simple command..."


so Tissot11's "cp -fv" turns into "cp -i -fv". It is a simple substition. The shell does not involve itself with command argument processing. It does not know what the argument rules are for a command. It just substitutes the command name with the alias contents, then sees if the newly formed command name is an internal command or a program in PATH. It then passes the arguments on the substituted command line to the program which has the knowledge to interpret the command line options.


Which goes a long way to explaining why Unix commands have such a diverse behavior with respect to options and what they mean, what they look like, etc...

Feb 6, 2013 2:52 PM in response to BobHarris

I thought the orginal poster was running the cp command from a script.


I get my alias command aa as undefined. Hence, he should have not seen the cp -i


mac $ bash -version

GNU bash, version 2.05b.0(1)-release (powerpc-apple-darwin8.4.0)

Copyright (C) 2002 Free Software Foundation, Inc.

mac $ cat new.sh

#!/bin/sh


# debug info

export PS4='+(${BASH_SOURCE}:${LINENO}):'

set -o xtrace


aa

mac $ alias aa

alias aa='ls'

mac $ ./new.sh

+(:7):aa

./new.sh: line 7: aa: command not found

mac $


----------------------------------

The leading backslash skips aliasing

\ls

the /bin/ skips function calls, I was just reading. As well as avoiding bath problems.

/bin/\ls

Feb 6, 2013 6:08 PM in response to BobHarris

Interesting. Its been some time since I mucked with this but Bob's explanation sounded reasonable and then I tested it out.


Seems there is a difference between how sh and bash handle this (now I know that the binary for sh and bash are identical on OS X but the behavior of the shell does change depending on if argv[0] is sh or bash)


If the script starts with /bin/bash no aliases are expanded, it it starts with /bin/sh aliases defined in the script are expanded.

Feb 7, 2013 11:07 AM in response to Frank Caggiano

Very Interesting. [ Unix ! ]


fyi

This make sense actually. It's to give the alias "cp=cp -i" precidence.


man cp

-f For each existing destination pathname, remove it and create a new

file, without prompting for confirmation regardless of its permis-

sions. (The -f option overrides any previous -n option, but not -i

option. See also the legacy section.)


LEGACY DESCRIPTION

When invoked in legacy mode, both -n, -i options are overridden by the -f

option.


------------------------------------


PS. I've have put the copy very near the top of the script. Seems like it would be good coding style to do a rm the copy if you expect to delete something.

.bashrc backup

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.