Issues with setting Aliases

Hi,

I have

set alias rm='rm -i' 

and i m able to see that in the list of aliases.

however, when i fire the rm command the '-i' interactive flag does not seem to kick in.

I login to a free online terminal and then i say bash to set the bash profile.

Join

Here is the output uploaded:

Please suggest a solution or another online terminal that help me do this.

Cant see the output, but I dont understand, where did/do you set your rm alias, after beeing in the session?

Sure you need the set ? In my .profile , I have

        alias rm='rm -i'

I dont find the .profile and i guess if you log onto the free online terminal you too would not be able to see it under your login.

I set the alias like this -> alias rm='rm -i' in the .bashrc and .bash_profile under my home directory.

I am also able to see rm=rm -i set as an alias under my profile; however, it does not seem to kick in.

You can try and let me know if your rm alias works... if so how on this online unix terminal -> Join

An looks like my snapshot image has been removed for some reason.

Hi,

Try the following;

-bash-3.2$ alias rm="rm -i"
-bash-3.2$ rm test
rm: remove test (yes/no)? yes
-bash-3.2$

Regards

Gull04

That suggestion made a lot of sense to me.

I did as you said Gull that works.

So the point is rm -rf does not prompt for confirmation while rm -r works and asks for confirmation. Also rm -irf also works.

Can anyone now explain why is this so and how to fix so that "-i" flag works with "-f" flag ?

Hi,

Could you post the output of the alias command?

Regards

Gull04

By default, webminal don't set "rm -i" as an alias to "rm" . You can verify this command

alias | grep "rm"

Above command won't give any alias. As other members mentioned, to set alias do:

alias rm="rm -i"

Then verify the alias:

alias | grep "rm"

Now create and delete a file:

touch a
rm a

This will be interactive delete.

If you want to add this alias to your session. Then edit your ".bash_profile" with above alias.

PS: I'm part of Webminal team, logged in to answer this question :slight_smile:

man rm:

Hi motashims,

I'm not entirely sure where the rm -if requirement came from, it wasn't here at the beginning of the thread.

But as RudiC says they are mutually exclusive, so can't be used together.

Regards

Gull04

I dont see this resolved as rm -if prefers -i over -f and asks for user confirmation while when i try to do the same thing by setting alias rm='rm -i' and then rm -f then it does not prefer -i over -f and does not ask for user confirmation which is strange / unexplained.

I take your word that -i and -f should not be used together but if used together why would the behaviour of what is being prefered change when using and not using aliases ?

Note: i m using single qoutes for setting alias and not double quotes. Could that be a problem ?

I will need another 8 hrs before i can paste the output from the terminal. But like i said you can test this issue yrself by registering and loggin to the online terminal -> Join

Hi,

It would probably be best if you were to read this.

Regards

Gull04

1 Like

I can't confirm this claim; tested on Linux and FreeBSD - no confirmation if -f is present, neither when using the alias nor stating the -i explicitly.

---------- Post updated at 14:52 ---------- Previous update was at 14:50 ----------

Mayhap lakshmipathi has more insight?

According to the standards, if rm is invoked with both -i and -f , the last one of those two options specified takes effect and the other option will be ignored. So:

rm -if other_options operands

should be treated exactly the same as:

rm -f other_options operands

and:

rm -fi other_options operands

and:

rm -f -i -f -ififif -i other_options operands

should be treated exactly the same as:

rm -i other_options operands

One of the reasons the standards require this is to allow a user to override one of these options set by an alias by just adding the desired option as an option when the alias is invoked.

I make no claims as to whether or not the version of rm you are using conforms to the standards.

I have final question after learning how flags work.

i m on Linux fedori 2.6.35.14 GNU/Linux

So, i can confirm that on my OS that which ever flag come later takes precedence. So

rm -fi works and asks for confirmation
rm -if does not work and force deletes the file.

Now when i set alias rm='rm -i' and then try rm -f it does not ask for confirmation and force deletes the file; meaning it populates the final command as rm -if

Do you know of any trick by which i can force interactive -i flag to kick in by setting it in the alias ? Somehow please ?

No. An alias cannot override what follows it on the command line.

You could write a replacement script for rm and install it in your PATH before the standard rm and have it examine its arguments and insert a -i before the first operand and then invoke the standard rm utility. But, if someone using your alias explicitly asks for -f to be used, why do you want to override that???

Even if rm -f wouldn't inhibit the -i - \rm would. It bypasses the alias.

Sorry, i did not get what you are trying to say here.

My question is that irrespective to the order of -i or -f flags the rm should always prefer -i and ask for confirmation. Can i somehow enforce this to happen ? Yes or No ? If Yes, How ?

No. And, there is no logical reason to want to do so.

Why do you think it is important to override a user not wanting the output produced by -i when that user explicitly asks for the behavior afforded by -f ?

If this is for an alias you are defining for your own use, why does it matter? Just don't type -f when you use the alias if you don't want to use -f ! If this is an alias you are creating for others to use, DO NOT ATTEMPT to thwart a user's attempt to get the behavior they need for the job they are doing.

@Cragun: We have naive users who are given access to our servers. They do not understand the significance of -f option and due to overlook they may specify a folder instead of a file becoz they overlooked the space charecter in the path to the file making it a folder path.

rm -irf /tmp/hello /output/bob.txt

This will accidentally delete the folder "hello" instead of the file "bob.txt" as they copy paste these path from word documents or emails.

So if i can impose the -i option for others as we use the same id they will be prompted and thus they can correct their mistake rather than the -f option doing the damage.

Your suggestions please... to my concern ?

Also, i was wondering where the same behavior of a later flag over riding the behavior of the previous flag could be tested.

I tried grep "Sunday" -wi hello.txt and grep "Sunday" -iw hello.txt but i get the same output where as grep "Sunday" -w hello.txt and grep "Sunday" -i hello.txt yields different results.

Can you tell me other commands and the flags that override each others behavior like the -i and -f flag over rides each other in case of rm command.