The tabulator in bash

Written by - 0 comments

Published on - Listed in Linux Shell Coding


I've just spent interesting 20mins with my boss, trying to figure out why Bash wouldn't recognize our command to not output lines containing tabulator entries.

The problem:

grep -v "\t" tempfile # does not work

Still shows lines with tab-entries. At first we thought maybe Bash has a problem recognizing the tabulator, so my idea was to use sed instead of grep:

sed -e '/\t/d' tempfile # works

It works with sed! So the \t is correctly recognized. Maybe a problem of grep only? After doing several tests, we finally found the solution. First we used an echo to force a tab sign within the grep command:

grep -v "$(echo -ne "\t")" tempfile # works

So as soon as a sub-shell is opened to 'translate' \t into a tab-sign it works, how it is also written in the bash-manpage:

Words of the form $'string' are treated specially.  The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:
...
\t     horizontal tab
...

Which leads to the following and final solution:

grep -v $'\t' /tmp/tempfile # works


Add a comment

Show form to leave a comment

Comments (newest first)

No comments yet.

RSS feed

Blog Tags:

  AWS   Android   Ansible   Apache   Apple   Atlassian   BSD   Backup   Bash   Bluecoat   CMS   Chef   Cloud   Coding   Consul   Containers   CouchDB   DB   DNS   Database   Databases   Docker   ELK   Elasticsearch   Filebeat   FreeBSD   Galera   Git   GlusterFS   Grafana   Graphics   HAProxy   HTML   Hacks   Hardware   Icinga   Icingaweb   Icingaweb2   Influx   Internet   Java   KVM   Kibana   Kodi   Kubernetes   LVM   LXC   Linux   Logstash   Mac   Macintosh   Mail   MariaDB   Minio   MongoDB   Monitoring   Multimedia   MySQL   NFS   Nagios   Network   Nginx   OSSEC   OTRS   Office   PGSQL   PHP   Perl   Personal   PostgreSQL   Postgres   PowerDNS   Proxmox   Proxy   Python   Rancher   Rant   Redis   Roundcube   SSL   Samba   Seafile   Security   Shell   SmartOS   Solaris   Surveillance   Systemd   TLS   Tomcat   Ubuntu   Unix   VMWare   VMware   Varnish   Virtualization   Windows   Wireless   Wordpress   Wyse   ZFS   Zoneminder   


Update cookies preferences