14

Just a quick question. Is it possible to tab the output on Ubuntu 16.04 LTS? I've created two images below to try and illustrate what I mean by this.

img1

img2

  • 1
    Would it be okay if you have to pipe the command output through another command like YOUR COMMAND | indent or if you have to type another command in front of it (could even be only an underscore) like _ YOUR COMMAND? – Byte Commander Jul 17 '16 at 14:41
  • 1
    @ByteCommander I have considered that, however I'm doing this for ease of use and to make console output easier to read. So I don't think piping my all of my commands is the solution. Thank you for the response and edit though! – Alex Ng Jul 17 '16 at 14:45
  • [Please don't post screeshots of text output. ](meta.askubuntu.com/q/8713/85695) – terdon Jul 18 '16 at 13:22
15

You could do something like this - with file descriptors:

  1. save the current output descriptor so we can restore it later

    exec 3>&1
    
  2. redirect the output stream to a process substitution that inserts the tab - for example

    exec 1> >(paste /dev/null -)
    

From this point, any process that writes to standard output will have that output 'filtered' through the paste command to insert tabs at the start.

  1. When you're done, you can resume normal behavior by restoring the saved file descriptor and closing the temporary one

    exec 1>&3 3>&-
    
  • I'd like to thank you for the response. This is the closest I've come to a solution even though it's not perfect. – Alex Ng Jul 18 '16 at 17:01
  • @AlexNg thanks for the upvote - in what way specifically doesn't it do what you asked for? if you let me know I will try to improve it – steeldriver Jul 18 '16 at 18:52
  • To give a few examples: After inputting your commands, some commands do work as intended such as 'php -v', 'hostname' and others. However, when using a command such as 'echo Hello World', the output is displayed inline with the input and it also adversely effects the input visual appearance when inputting the next command. Some other commands are also no effected by the tabbing process, such as when inputting an invalid command such as 'asdasd'. – Alex Ng Jul 18 '16 at 19:07
  • Finally, it does not correctly display output that changes (not sure how to describe this properly, apologies). For example, if the command 'apt-get update' is used, there are noticeable differences when comparing it to a console without the tabbing process. – Alex Ng Jul 18 '16 at 19:07
  • The problem is that this approach only handles STDOUT (stream 1), but not STDERR (stream 2). It does also break the output of programs that use less or similar tools to allow scrolling of the output, like e.g. man. – Byte Commander Jul 18 '16 at 22:09
8

I understand this answer is not optimal for you, but you could make a Bash function with the simple name _ (or anything else that is not used yet) which runs the command it gets as arguments and indents all of its output with a tab.

An example:

$ _ lsb_release -a
        No LSB modules are available.
        Distributor ID: Ubuntu
        Description:    Ubuntu 16.04 LTS
        Release:    16.04
        Codename:   xenial

The code of this function would only be this:

_(){ sed "s/^/\t/" <($*); }

You can append it to your ~/.bashrc file so that it will be available in all Bash sessions of your user by running:

echo '_(){ sed "s/^/\t/" <($*); }' >> ~/.bashrc

If you want to make it available to all users, append it to /etc/bash.bashrc instead (requires admin rights):

echo '_(){ sed "s/^/\t/" <($*); }' | sudo tee -a /etc/bash.bashrc

Alternatively you could also create a script file like /usr/local/bin/_ with the following content to achieve the same without Bash functions and for all users:

#!/bin/bash
sed "s/^/\t/" <($*)

You must make the script executable after saving the file using

sudo chmod +x /usr/local/bin/_
5

With echo specifically, you can have it interpret backslash escapes like \t (a tab stop) with the -e switch:

echo -e "\tHello World"

But it would hardly be feasible (or probably possible) to modify the output the of every command that way. If ease of read is your goal, you might want to look into customizing the look (e. g. the coloring) of your command prompt, like shown here:

You can customize your prompt by changing the content of the environment variable PS1. You can see a little example of how to do that in the blog post the above screenshot is from. How to use those so called ANSI escape sequences in detail, how to use tools that are more comfortable, how to have the changes applied by default in new terminals etc. are beyond the scope of this answer. But if you decide to use that technique, there are a lot of tutorials and further information around the web.

  • Thanks for the response, however I've already changed PS1 to my liking. – Alex Ng Jul 17 '16 at 15:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.