print command instead of echo in linux

148,177

Solution 1

You're confusing many things here, let me try and tease this apart for you.

  1. awk '{print}' ...

    This has nothing to do with the print you're asking about. awk is a dynamic programming language which contains its own commands, of which print is one of them.

  2. print --"text/plain" "prayag works on JVM" and other forms

    The command print is for displaying files per the contents of the mailcap file, using the designated "handler" for each particular file type, not for printing strings to the screen.

    excerpt from the print man page

    run-mailcap, view, see, edit, compose, print - execute programs via entries in the mailcap file

    run-mailcap (or any of its aliases) will use the given action to process each mime-type/file in turn. Each file is spec‐ ified as its mime-type, its encoding (e.g. compression), and filename together, separated by colons. If the mime-type is omitted, an attempt to determine the type is made by trying to match the file's extension with those in the mime.types files. If the encoding is omitted, it will also be determined from the file's extensions. Currently supported encodings are gzip (.gz), bzip (.bz), bzip2 (.bz2), and compress (.Z). A filename of "-" can be used to mean "standard input", but then a mime-type must be specified.

  3. echo

    The command echo is a builtin command to the shell you're using called Bash (I'm assuming you're using Bash). You can confirm this by using this command:

    $ type -a echo
    echo is a shell builtin
    echo is /bin/echo
    

    You'll notice that echo is also a standalone executable, /bin/echo. This is a different command than the echo you're using, but serves a similar purpose.

  4. print in shells such as zsh/ksh

    Thanks to @Gilles comment, the print command you're looking for is most likely the one that is built into shells such as zsh and/or ksh. You can see its usage through the man page, man zshbuiltins.

    Example

    zsh % print "hello world"
    hello world
    

    You can run one of these shells by typing zsh or ksh.

printf

Perhaps you're looking for the command printf?

$ printf "%s\n" "hello stringy world"
hello stringy world

$ printf "this is some padded digits: %04d\n" "10"
this is some padded digits: 0010

The command printf takes a formatting set of special characters so that you can instruct it how you want to display things such as strings or digits etc. See the man page for printf.

mailcap

So a lot of people are probably oblivious as to the file /etc/mailcap. This file contains mime-types (headers from files which identifies what type of file a file is). You can then create entries in this mailcap file which designate what tool to use to open a particular mime-type for a file.

Example

text/html; /usr/bin/sensible-browser '%s'; description=HTML Text; nametemplate=%s.html
application/x-troff-man; /usr/bin/nroff -mandoc -Tutf8; copiousoutput; print=/usr/bin/nroff -mandoc -Tutf8 | print text/plain:-
application/x-ogg; /usr/bin/mplayer '%s'; description="OggVorbis Audio"
application/ogg; /usr/bin/mplayer '%s'; description="OggVorbis Audio"
audio/mpeg; /usr/bin/mplayer '%s'; description="MPEG Audio Format"
audio/x-mpegurl; /usr/bin/mplayer '%s'; description="Icecast Playlists"
audio/x-ms-wax; /usr/bin/mplayer '%s'; description="Audio Format"

The above lines say that if you get a file with the mime-type audio/mpeg, then use the tool /usr/bin/mplayer to open this file.

These rules are generally used by the email tool, but other tools can take advantage of these entries within the mailcap file as well.

If you're interested in learning more about mime-types or mailcap see the following references.

References

Solution 2

You can use printf command to do so,

 # printf Hello World
 Hello

 # printf "Hello World"
 Hello World

Well, exatly you need to use print commnad then you can add alias

 # alias print='echo'

 # print Hello World
 Hello World

 # unalias print  < -- to revert.

also to set permanent,

 # export print='echo'

 # print Hello Linux
 Hello Linux

so,

# export | grep print
declare -x print="echo"

# unset print   < -- to revert.
# export | grep print
Share:
148,177

Related videos on Youtube

prayagupa
Author by

prayagupa

(def summary[] (:TCP/IP-socket-programmer "who loves to send and receive bits and bytes") (:using "bytecode instructions which runs on JVM [clojure, groovy, java12]") (and ([sql, nosql])) (also (did [PHP, CLR] socket programming once upon a time)) (does mobile app programming sometimes in [Android] Platform.) (TDD practitioner)) (def resume[] {:stackoverflow_careers "http://careers.stackoverflow.com/prayagupd" ))

Updated on September 18, 2022

Comments

  • prayagupa
    prayagupa almost 2 years

    I simply can print a message in terminal using linux echo command.

    prayag@prayag$ echo "prayag works on JVM"
    prayag works on JVM
    

    Can I have the same output with print command.

    I actually went through their manuals, where $ man print describes it as Run-mailcap-programs which term I never heard before. And came to know that it is used to find the correct program to open a file with, based on MIME.

    So is there any way to print a simple line using print alone instead of echo?

    Found similar kind of problem at Need to assign the contents of a text file to a variable in a bash script, but people suggesting echo over print there.

    I tried following, but got issues.

    $ print --"text/plain" "prayag works on JVM"
    Warning: unknown mime-type for "prayag works on JVM" -- using "application/octet-stream"
    Error: no such file "prayag works on JVM"
    

    It asks for a file, with file provided.

    $ print --"text/plain" application.properties 
    Warning: unknown mime-type for "application.properties" -- using "application/octet-stream"
    Error: no "print" mailcap rules found for type "application/octet-stream"
    

    But got working in combination with awk command with a file provided,

    $ awk '{print}' application.properties 
    prayag works on JVM
    
    • slm
      slm over 10 years
      What OS are you on?
    • prayagupa
      prayagupa over 10 years
      @slm I'm on ubuntu
    • slm
      slm over 10 years
      OK, see my answer and let me know if it helps