开发者

Why do some commands behave differently when run using a bash alias?

开发者 https://www.devze.com 2023-03-16 17:43 出处:网络
In developing with SVN, sometimes I want to just rollback all my changes and start fresh, so I put together a command to revert all the files in my checkout:

In developing with SVN, sometimes I want to just rollback all my changes and start fresh, so I put together a command to revert all the files in my checkout:

alias "svn-reset"="svn status | perl -nale 'print $F[1] if /^M/' | xargs svn revert"

When I run svn status | perl -nale 'print $F[1] if /^M/' | xargs svn revert on the command line, the code works as expected: all my modified files get reverted, as verified with another svn status command. However, when I run svn-reset, I get output like the following:

$ svn-reset
Skipped 'ARRAY(0x1c2f52a0)'
Skipped 'ARRAY(0x1c2f5450)'
Skipped 'ARRAY(0x1c2f5410)'

In this example, I've verified that I have three modified files in my checkout, so it looks like the problem is with perl printing out the wrong information. However, I know hardly any perl, and I'm stumped as to why perl would behave differently run through a bash alias as compared to running the same perl code manually. Any thoughts?

This question is about why perl behaves differently when run using a bash alias. If anyone has any suggestions for a more efficient way to automatically revert all modified files in SVN, I'd be interested in th开发者_StackOverflow社区at too, but that doesn't answer my question.


Try escaping the dollar, so that the shell does not substitute $F with nothing when you create the alias:

alias svn-reset="svn status | perl -nale 'print \$F[1] if /^M/' | xargs svn revert"


This is how your alias looks like to the shell

output from alias command:

alias svn-reset='svn status | perl -nale '\''print [1] if /^M/'\'' | xargs svn revert'

after escaping the $, it looks like this instead:

alias svn-reset='svn status | perl -nale '\''print $F[1] if /^M/'\'' | xargs svn revert'


You used double quotes around the alias, so the shell is expanding the $F. Change it to (untested):

alias "svn_reset"="svn status | perl -nale 'print \$F[1] if /^M/' | xargs svn revert"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号