LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-06-2018, 11:12 PM   #1
errigour
Member
 
Registered: May 2009
Posts: 366

Rep: Reputation: 6
echo like printf


is there a way I can echo a value like printf would print it?
like if I did
printf("%3d", variable);
How can I do that with echo is it possible?
 
Old 08-06-2018, 11:26 PM   #2
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
Why would you want to ?. Bash (at least) has printf.
 
Old 08-06-2018, 11:43 PM   #3
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
oh I didn't know that could you show me an example of using the variable $i printed with printf?
Im a novice at this stuff.
 
Old 08-06-2018, 11:47 PM   #4
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,129

Rep: Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121Reputation: 4121
The info on bash builtin commands can be a little hard to dig out - try "help printf" in this case.
Code:
z=3 ; printf "%04d\n" $z
 
Old 08-06-2018, 11:52 PM   #5
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
got it heres what I did
Code:
printf "case %3d: return %s;\n" "$i" "$element"
 
Old 08-06-2018, 11:53 PM   #6
errigour
Member
 
Registered: May 2009
Posts: 366

Original Poster
Rep: Reputation: 6
thanx
 
Old 08-07-2018, 09:08 AM   #7
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
I would suggest using proper quoting, note that "\n" is a literal new line while \\n, '\n' and "\\n" are not.

I personally use printf like this when simply printing output, it seems to work everywhere and is relatively fool proof.

Code:
$ foo=foo
$ bar=bar
$ printf %s\\n "$foo" "$bar"
foo
bar
 
Old 08-07-2018, 03:49 PM   #8
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by orbea View Post
note that "\n" is a literal new line while \\n, '\n' and "\\n" are not.
That’s not true and you can verify it by running echo "\n". Inside of double quotes, backslash has special meaning only in front of dollar, back-tick, apostrophe, backslash or new-line character. See section 2.2.3 in Shell Command Language or Quoting section in bash man page. All of the sequences you’ve listed are equivalent.
 
Old 08-08-2018, 03:00 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,866
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Off-topic note: even printf is platform-dependent: in some version, you only have \033 to print ESC character; in others \e and/or \x1b also works.
 
Old 08-08-2018, 11:00 AM   #10
orbea
Senior Member
 
Registered: Feb 2015
Distribution: Slackware64-current
Posts: 1,950

Rep: Reputation: Disabled
Quote:
Originally Posted by mina86 View Post
That’s not true and you can verify it by running echo "\n". Inside of double quotes, backslash has special meaning only in front of dollar, back-tick, apostrophe, backslash or new-line character. See section 2.2.3 in Shell Command Language or Quoting section in bash man page. All of the sequences you’ve listed are equivalent.
Thanks for the correction, I was mixing up things.

Edit: I see I was recalling this shellcheck warning, but it has since been retired due to being too pedantic.

https://github.com/koalaman/shellcheck/wiki/SC1117

Last edited by orbea; 08-08-2018 at 11:05 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] echo vs printf speed Linux_Kidd Programming 10 10-24-2011 10:39 PM
need to echo / printf string containing date as parameter in cron v8625 Linux - Newbie 1 02-25-2011 06:31 PM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
php, printf vs echo snowtigger Programming 3 04-17-2009 02:47 AM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:10 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration