Pass length of argument into bash command substitution
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I'm trying to write a quick bash function that populates a README.md with a $1n
followed by underscores the length of $1
.
The code I found in other stackexchange questions showed that to print a character <n>
times, use
printf '=%.0s' 1..<n>
and indeed, this works (obviously replacing <n>
with a number).
To create my README.md, I thought the function would look something like this:
make_readme()
echo "$1
$(printf '=%.0s' 1..$#1)" > README.md
make_readme "Some project"
This, however, produces a file with this text:
Some project
=
As far as I can tell, $#1
within the $(...)
is being replaced with the empty string. My guess is that command substitutions get their own argument scopes, and since there are no arguments passed to the substitution, $1
is being replaced with nothing.
I did finally finagle a couple workarounds:
make_readme()
underline="printf '=%.0s' 1..$#1"
echo "$1
$(eval "$underline")" > README.md
or
make_readme()
echo "$1" > README.md
printf '=%.0s' 1..$#1 >> README.md
but it seems like there should be a way to do this in one line.
bash shell-script command-substitution
New contributor
add a comment |Â
up vote
0
down vote
favorite
I'm trying to write a quick bash function that populates a README.md with a $1n
followed by underscores the length of $1
.
The code I found in other stackexchange questions showed that to print a character <n>
times, use
printf '=%.0s' 1..<n>
and indeed, this works (obviously replacing <n>
with a number).
To create my README.md, I thought the function would look something like this:
make_readme()
echo "$1
$(printf '=%.0s' 1..$#1)" > README.md
make_readme "Some project"
This, however, produces a file with this text:
Some project
=
As far as I can tell, $#1
within the $(...)
is being replaced with the empty string. My guess is that command substitutions get their own argument scopes, and since there are no arguments passed to the substitution, $1
is being replaced with nothing.
I did finally finagle a couple workarounds:
make_readme()
underline="printf '=%.0s' 1..$#1"
echo "$1
$(eval "$underline")" > README.md
or
make_readme()
echo "$1" > README.md
printf '=%.0s' 1..$#1 >> README.md
but it seems like there should be a way to do this in one line.
bash shell-script command-substitution
New contributor
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm trying to write a quick bash function that populates a README.md with a $1n
followed by underscores the length of $1
.
The code I found in other stackexchange questions showed that to print a character <n>
times, use
printf '=%.0s' 1..<n>
and indeed, this works (obviously replacing <n>
with a number).
To create my README.md, I thought the function would look something like this:
make_readme()
echo "$1
$(printf '=%.0s' 1..$#1)" > README.md
make_readme "Some project"
This, however, produces a file with this text:
Some project
=
As far as I can tell, $#1
within the $(...)
is being replaced with the empty string. My guess is that command substitutions get their own argument scopes, and since there are no arguments passed to the substitution, $1
is being replaced with nothing.
I did finally finagle a couple workarounds:
make_readme()
underline="printf '=%.0s' 1..$#1"
echo "$1
$(eval "$underline")" > README.md
or
make_readme()
echo "$1" > README.md
printf '=%.0s' 1..$#1 >> README.md
but it seems like there should be a way to do this in one line.
bash shell-script command-substitution
New contributor
I'm trying to write a quick bash function that populates a README.md with a $1n
followed by underscores the length of $1
.
The code I found in other stackexchange questions showed that to print a character <n>
times, use
printf '=%.0s' 1..<n>
and indeed, this works (obviously replacing <n>
with a number).
To create my README.md, I thought the function would look something like this:
make_readme()
echo "$1
$(printf '=%.0s' 1..$#1)" > README.md
make_readme "Some project"
This, however, produces a file with this text:
Some project
=
As far as I can tell, $#1
within the $(...)
is being replaced with the empty string. My guess is that command substitutions get their own argument scopes, and since there are no arguments passed to the substitution, $1
is being replaced with nothing.
I did finally finagle a couple workarounds:
make_readme()
underline="printf '=%.0s' 1..$#1"
echo "$1
$(eval "$underline")" > README.md
or
make_readme()
echo "$1" > README.md
printf '=%.0s' 1..$#1 >> README.md
but it seems like there should be a way to do this in one line.
bash shell-script command-substitution
bash shell-script command-substitution
New contributor
New contributor
New contributor
asked 7 mins ago
dfoverdx
1012
1012
New contributor
New contributor
add a comment |Â
add a comment |Â
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
dfoverdx is a new contributor. Be nice, and check out our Code of Conduct.
dfoverdx is a new contributor. Be nice, and check out our Code of Conduct.
dfoverdx is a new contributor. Be nice, and check out our Code of Conduct.
dfoverdx is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f476423%2fpass-length-of-argument-into-bash-command-substitution%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password