set existing_enc [encoding system] #Any "puts" before setting 'encoding system' will set the existing system encoding on the channel (others too, but depending if console vs piped) #e.g ### puts stderr test #Uncommenting the above will mean that both stdout and stderr (when in a piped-situation, ie no console) are initialised to existing_enc - not the one we set below! set arg_setencoding [lindex $::argv 0] if {$arg_setencoding ne ""} { if {$arg_setencoding ni [encoding names]} { puts stderr "Usage: encoding.tcl ?tcl_encoding?" puts stderr "(Note difference in stdout/stderr encodings when piped: e.g encoding.tcl cp437 | cat)" puts stderr "encoding names:\n" puts stderr "[encoding names]" exit 1 } encoding system $arg_setencoding } else { encoding system utf-8 } puts "original encoding system : $existing_enc" puts "configured encoding system: [encoding system]" puts "stdout: [chan conf stdout]" puts "stderr: [chan conf stderr]" puts "[lindex $::argv 0]" #compare: #1) both stderr and stdout are to console - not affected by changed system encoding #>tclsh encoding.tcl # original encoding system : utf-8 # configured encoding system: utf-8 # stdout: -blocking 1 -buffering line -buffersize 4096 -encoding utf-16 -eofchar {} -profile tcl8 -translation crlf -winsize {224 57} # stderr: -blocking 1 -buffering none -buffersize 4096 -encoding utf-16 -eofchar {} -profile tcl8 -translation crlf -winsize {224 57} #2) stdout not going to console #>tclsh encoding.tcl | cat # original encoding system : utf-8 # configured encoding system: utf-8 # stdout: -blocking 1 -buffering line -buffersize 4096 -encoding utf-8 -eofchar {} -profile tcl8 -translation crlf # stderr: -blocking 1 -buffering none -buffersize 4096 -encoding utf-16 -eofchar {} -profile tcl8 -translation crlf -winsize {224 57} #3) neither channel to console #>tclsh encoding.tcl |& cat # original encoding system : utf-8 # configured encoding system: utf-8 # stdout: -blocking 1 -buffering line -buffersize 4096 -encoding utf-8 -eofchar {} -profile tcl8 -translation crlf # stderr: -blocking 1 -buffering none -buffersize 4096 -encoding utf-8 -eofchar {} -profile tcl8 -translation crlf