ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
In case 1, the first (and only) argument starts with a blank, and so it is treated as a simple text string. In cases 2 & 3, however, it starts with the hyphen, meaning that the command tries to treat it as an option flag.
The '--' in case 2 is treated as the "last option" option, clearing the way for the non-option arguments. But since there are no following arguments it uses the default 'y'
In case 3 '- ' is simply an invalid argument.
The way around it is to always use '--' first when the first non-option argument starts with a hyphen.
Code:
yes -- '--' |head -n4
yes -- '- ' |head -n4
This is true of most commands. You'd do the same, for example, if you needed to rm a filename that started with a hyphen. See the common options section of info coreutils.
Edit: BTW, here's a quick&dirty way to get the same output using bash only. At least with low numbers of lines.
Code:
printf '%s\n' '- '{,,,}
The brace expansion expands into one hyphen string for each comma-separated blank space.
Last edited by David the H.; 03-09-2013 at 12:29 PM.
Reason: some additions
A follow-on question... I was groping around hoping to find a Linux equivalent for the REXX built-in function COPIES, and thought yes would serve. As you explained, that works for many character strings but not all.
This is how COPIES works:
Code:
COPIES('FOO',3) returns 'FOOFOOFOO' (Without the quote marks)
COPIES('*',16) returns '****************' (Without the quote marks)
COPIES('Bar ',2) returns 'Bar Bar ' (Without the quote marks)
Is there a Linux command which suits this purpose?
I would be curious, does REXX's function print the original examples as expected? I would assume yes as it is not shell environment using a command, such as yes.
Sorry Daniel, I was unclear, I meant the dash examples in the original question, but as I said, I would guess it would not be affected as it is not a command with switches, which is why "yes" was affected.
Edit: Here's the same thing wrapped up in a small function:
Code:
#$1 is the string to copy
#$2 is the number of times to print it (defaults to one)
copies(){
local str
printf -v str '%0*d' "${2:-1}"
printf '%s\n' "${str//0/$1}"
}
The use of "*" in the %d format string tells it to use the value of the first argument as the padding number, rather than as a printing input.
Last edited by David the H.; 03-12-2013 at 09:10 AM.
Reason: as stated
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.