bash script remove non-printable special characters/white space

In bash you test like this for problems that can be caused by non-printable characters since if you don't know how to identify it because it basically breaks your script.

echo "url=$url" should print something like this normally if you have properly formatted input/text:

url=::http://someaddress.com::

But if you have some weird hidden characters (not visible in a text editor of any sort you'll get something like this
::l=::http://someaddress.com

How come it doesn't print as expected?
There are hidden/non-printable characters, usually ASCII code I believe that cause unexpected behavior like above and wreaks havoc on many scripts/programs who didn't realize this since there are non-printable characters.

There is a program in Linux that takes care of it called "tr"
If I read data line by line using cat, I now use "tr" to filter all the non-printable characters (I use it line by line and not the entire file since this would remove things like linebreaks and then you have a text file that becomes a single line).

Here is how you'd fix a bad variable (one with non-printable/special characters):

url=`echo "$url"|tr -dc ‘[:print:]‘


Tags:

bash, printable, characters, spacein, echo, quot, url, formatted, input, text, http, someaddress, visible, ll, doesn, ascii, wreaks, havoc, scripts, programs, didn, linux, tr, filter, linebreaks, variable, dc, lsquo,

Latest Articles

  • Cisco Switches How To Get Of Port Line Status Console Messages
  • Cisco DHCP Snooping Relay Setup Information
  • Cisco Switch Setup Guide Command List
  • Cisco 2960 Switch Reset To Factory Defaults
  • How To Boot Cisco CUCM UCSInstall 8.6, 10, 11 and 12 on KVM/Proxmox
  • VBOX VirtualBox How To Import Raw .img Disk File
  • Windows Server 2012, 2016, 2019 How To Install and Missing Disabled Telnet Client
  • proxmox vm networking breaks when you restart your network on the hostnode
  • Linux ln symlink how to update existing symbolic link
  • Ubuntu 18.04 / Linux Mint 19.1 Cannot Type or Login - solution
  • LUKS Hard Drive Encryption on Linux Mint Ubuntu Debian etc how to mount encrypted hard drive
  • How to use nmap locate other machines/computers/servers on your network using nmap
  • Linux Mint 18.2 Create Config File To Start Application Upon Login
  • Dell Wyse Thin Client BIOS Access Key
  • sudoers file in /etc warning about comments/includes!
  • Centos 7 Reallocate logical volume space to another
  • lvm how to reduce volume size
  • letsencrypt certbot error "Unable to find a virtual host listening on port 80 which is currently needed for Certbot to prove to the CA that you control your domain. Please add a virtual host for port 80."
  • SSH error cannot Forward or Listen "bind: Cannot assign requested address"
  • X11 SSH Linux Forwarding Error