How to align a string variable in Bash

Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I'm trying to align a string variable in Bash. This is the desired behaviour.
if str1="123" , then str2=" 123"
if str1="1234" , then str2=" 1234"
if str1="12345", then str2=" 12345"
etc.
I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.
bash text-formatting
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
I'm trying to align a string variable in Bash. This is the desired behaviour.
if str1="123" , then str2=" 123"
if str1="1234" , then str2=" 1234"
if str1="12345", then str2=" 12345"
etc.
I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.
bash text-formatting
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to align a string variable in Bash. This is the desired behaviour.
if str1="123" , then str2=" 123"
if str1="1234" , then str2=" 1234"
if str1="12345", then str2=" 12345"
etc.
I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.
bash text-formatting
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to align a string variable in Bash. This is the desired behaviour.
if str1="123" , then str2=" 123"
if str1="1234" , then str2=" 1234"
if str1="12345", then str2=" 12345"
etc.
I've seen how to do it to print with printf, but I need to do it inside the variable without printing it out.
bash text-formatting
bash text-formatting
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 6 mins ago
Jeff Schaller
35.1k952115
35.1k952115
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 1 hour ago
Jairo Alves
61
61
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Jairo Alves is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
3
down vote
Assuming the variable only contains single-byte, single-width characters:
printf -v str2 %8s "$str1"
If they may contain multi-byte (but still single-width) characters, you can do instead:
printf -v str2 %8s%s '' "$str1"
str2=$str2: -8
(note however that that one truncates values larger than 8 characters).
Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:
str2=$(printf %8s "$str1")
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the-vmakes theprintfcommand abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
â Jairo Alves
1 hour ago
@JairoAlves, yes. Seeinfo bash printforhelp printfwithinbashfor details.
â Stéphane Chazelas
1 hour ago
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
3
down vote
Assuming the variable only contains single-byte, single-width characters:
printf -v str2 %8s "$str1"
If they may contain multi-byte (but still single-width) characters, you can do instead:
printf -v str2 %8s%s '' "$str1"
str2=$str2: -8
(note however that that one truncates values larger than 8 characters).
Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:
str2=$(printf %8s "$str1")
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the-vmakes theprintfcommand abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
â Jairo Alves
1 hour ago
@JairoAlves, yes. Seeinfo bash printforhelp printfwithinbashfor details.
â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
3
down vote
Assuming the variable only contains single-byte, single-width characters:
printf -v str2 %8s "$str1"
If they may contain multi-byte (but still single-width) characters, you can do instead:
printf -v str2 %8s%s '' "$str1"
str2=$str2: -8
(note however that that one truncates values larger than 8 characters).
Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:
str2=$(printf %8s "$str1")
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the-vmakes theprintfcommand abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
â Jairo Alves
1 hour ago
@JairoAlves, yes. Seeinfo bash printforhelp printfwithinbashfor details.
â Stéphane Chazelas
1 hour ago
add a comment |Â
up vote
3
down vote
up vote
3
down vote
Assuming the variable only contains single-byte, single-width characters:
printf -v str2 %8s "$str1"
If they may contain multi-byte (but still single-width) characters, you can do instead:
printf -v str2 %8s%s '' "$str1"
str2=$str2: -8
(note however that that one truncates values larger than 8 characters).
Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:
str2=$(printf %8s "$str1")
Assuming the variable only contains single-byte, single-width characters:
printf -v str2 %8s "$str1"
If they may contain multi-byte (but still single-width) characters, you can do instead:
printf -v str2 %8s%s '' "$str1"
str2=$str2: -8
(note however that that one truncates values larger than 8 characters).
Here bash's printf builtin command supports -v to store the result of printf into a variable, but even if it didn't you could use command substitution:
str2=$(printf %8s "$str1")
edited 1 hour ago
answered 1 hour ago
Stéphane Chazelas
292k54543882
292k54543882
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the-vmakes theprintfcommand abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
â Jairo Alves
1 hour ago
@JairoAlves, yes. Seeinfo bash printforhelp printfwithinbashfor details.
â Stéphane Chazelas
1 hour ago
add a comment |Â
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the-vmakes theprintfcommand abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?
â Jairo Alves
1 hour ago
@JairoAlves, yes. Seeinfo bash printforhelp printfwithinbashfor details.
â Stéphane Chazelas
1 hour ago
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
Thank you, very much! I'm trying it now and it will most probably do what I needed.It will help a lot! :)
â Jairo Alves
1 hour ago
If I'm understanding it correctly, the
-v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?â Jairo Alves
1 hour ago
If I'm understanding it correctly, the
-v makes the printf command abstain from printing the result on the screen, instead attributting it to the variable that follows it, is that correct?â Jairo Alves
1 hour ago
@JairoAlves, yes. See
info bash printf or help printf within bash for details.â Stéphane Chazelas
1 hour ago
@JairoAlves, yes. See
info bash printf or help printf within bash for details.â Stéphane Chazelas
1 hour ago
add a comment |Â
Jairo Alves is a new contributor. Be nice, and check out our Code of Conduct.
Jairo Alves is a new contributor. Be nice, and check out our Code of Conduct.
Jairo Alves is a new contributor. Be nice, and check out our Code of Conduct.
Jairo Alves 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%2f480170%2fhow-to-align-a-string-variable-in-bash%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