1

My question may not be clear enough, so here's a specific example.

I have an rsync bin\bash script with a ton of option switches.

Instead of:

rsync --recursive --times --update --chown=root,root --bwlimit=500

I'd like to be able to write my script like this:

rsync
--recursive 
--times 
--update 
--chown=root,root 
--bwlimit=500

That way, I can easily comment out or edit individual options if I need to.

Is there any way to do this?

1

2 Answers 2

3

Yep. Just escape the end of line with a \. Like this:

rsync \
  --recursive \
  --times \
  --update \
  --chown=root,root \
  --bwlimit=500

The amount of whitespace is irrelevant, the important thing is having the \ as the last character. So you can even align them all nicely like this:

rsync               \
  --recursive       \
  --times           \
  --update          \
  --chown=root,root \
  --bwlimit=500

Note that there can be no whitespace (or anything else) after the \, it must be the last character.

1
  • I tried this. Seems to do mostly what I want, but it seems it still doesn't allow you to easily comment out (or just comment) about particular lines. For example, I'd like to be able to do rsync \ #--verbose \ --recursive and then be able to remove the comment on --verbose as needed, but it seems that # isn't really compatible with the `` continuation marker.
    – Daniel
    Commented Jul 3, 2020 at 4:00
0

Can't speak for other linux distributions but,

in RHEL/CentOS 7 I like creating

/etc/profile.d/myaliases.sh

and in your case would do

alias rsync2='rsync --recursive --times --update --chown=root,root --bwlimit=500'

change rsync2 to whatever you like, then all you need to do is type rsync2

3
  • Eeeek! Please don't! Use ~/.profile or ~/.bashrc for this, don't modify global files! That's more dangerous, more intrusive if you have multiple users and much harder to maintain across updates. That said, this is about using a command in scripts and making it more legible. Using an alias won't help, especially not if you define it in a file that isn't read by scripts. So this isn't really answering the question.
    – terdon
    Commented Jul 2, 2020 at 18:24
  • please look into what profile.d/ folder is about. This is not modifying global files. It is a simple alias. You could do this in your script as a local alias, or you could make it available to all by making use of profile.d. Or you could just be miserable and type it all out every time. I like typing less characters when possible.
    – ron
    Commented Jul 4, 2020 at 0:29
  • I know what it is, thank you. And as you said, that affects all users on a machine. That's what global means. If you want to set an alias, use your local files not global ones. It's safer, less intrusive and easier to maintain. But anyway, again, you're not answering the question here. Using an alias wouldn't work in a script (unless you explain how to enable them and make sure the script is executed in an anvironment that has read the profile files), so it isn't a way of formatting a long command.
    – terdon
    Commented Jul 4, 2020 at 12:58

Not the answer you're looking for? Browse other questions tagged .